实现效果
实现原理
为recyclerview设置GridLayoutManager,通过重写adapter中的onAttachedToRecyclerView方法,在该方法中使用GridLayoutManager.setSpanSizeLookup方法获取每个item所占用的列;并在自定义recyclerview.adapter文件中根据viewtype加载不同的布局。
- step1:根据需要写出3个item布局文件(布局比较简单,就是普通的item布局文件,此处就不再贴出)
- step2:创建Adapter文件
public class Weather_Details_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<Weather.HeWeather5Bean.DailyForecastBean> daily_list;
private Weather.HeWeather5Bean.SuggestionBean suggestion;
private Weather weather;
private Weather.HeWeather5Bean.NowBean now;
private List<HashMap<String,String>> mSuggest=new ArrayList<>();
private int item_size;
public void setContext(Context context) {
this.context = context;
}
public void setWeather(Weather weather) {
this.weather=weather;
daily_list=weather.getHeWeather5().get(1).getDaily_forecast();
suggestion=weather.getHeWeather5().get(1).getSuggestion();
now=weather.getHeWeather5().get(1).getNow();
mSuggest=getSuggest();
item_size=daily_list.size()+6;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layout=LayoutInflater.from(context);
//此处的viewtype来自于getItemViewType(int position)方法返回的值;
//根据不同的viewtype,此处返回对应的holder
switch (viewType){
case Variable.weather_item_type_1:
return new Weather_Holder(layout.inflate(R.layout.now_weather_item,parent,false));
case Variable.weather_item_type_2:
return new Day_Holder(layout.inflate(R.layout.day_weather_item,parent,false));
default:
return new Live_Holder(layout.inflate(R.layout.live_weather_item,parent,false));
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//此处为不同的holder对象绑定相对应的数据
if (holder instanceof Weather_Holder){
((Weather_Holder) holder).mCity.setText(weather.getHeWeather5().get(1).getBasic().getCity());
((Weather_Holder) holder).msuggest.setText(suggestion.getSport().getTxt());
((Weather_Holder) holder).mTemp.setText(now.getTmp());
((Weather_Holder) holder).mWeather.setText(daily_list.get(0).getCond().getTxt_d()+"~"+
daily_list.get(0).getCond().getTxt_n());
((Weather_Holder) holder).mtemperature.setText(daily_list.get(0).getTmp().getMax()+"~"+daily_list.get(0).getTmp().getMax());
((Weather_Holder) holder).mDate.setText(weather.getHeWeather5().get(1).getBasic().getUpdate().getLoc());
((Weather_Holder) holder).mAir.setText(weather.getHeWeather5().get(1).getAqi().getCity().getQlty());
}else if (holder instanceof Day_Holder){
((Day_Holder) holder).mDate.setText(daily_list.get(position).getDate());
((Day_Holder) holder).mTemp.setText(daily_list.get(position).getTmp().getMax()+"~"+
daily_list.get(position).getTmp().getMin());
((Day_Holder) holder).mWeather.setText(daily_list.get(position).getCond().getTxt_d()+"转"
+daily_list.get(position).getCond().getTxt_n());
((Day_Holder) holder).mWind.setText(daily_list.get(position).getWind().getDir()+"-"+
daily_list.get(position).getWind().getSc());
}else if (holder instanceof Live_Holder){
((Live_Holder) holder).mTitle.setText(mSuggest.get(position-3).get("title"));
((Live_Holder) holder).mResult.setText(mSuggest.get(position-3).get("result"));
((Live_Holder) holder).mContent.setText(mSuggest.get(position-3).get("content"));
}
}
private List<HashMap<String,String>> getSuggest(){
String[] title=new String[]{context.getString(R.string.comfort),context.getString(R.string.car),
context.getString(R.string.dress),context.getString(R.string.cold),context.getString(R.string.trvavel),
context.getString(R.string.ultraviolet)};
String[] result=new String[]{suggestion.getComf().getBrf(),suggestion.getCw().getBrf(),suggestion.getDrsg().getBrf(),
suggestion.getFlu().getBrf(),suggestion.getTrav().getBrf(),suggestion.getUv().getBrf()};
String[] content=new String[]{suggestion.getComf().getTxt(),suggestion.getCw().getTxt(),suggestion.getDrsg().getTxt(),
suggestion.getFlu().getTxt(),suggestion.getTrav().getTxt(),suggestion.getUv().getTxt()};
for (int i=0;i<6;i++){
HashMap<String,String> map=new HashMap<>();
map.put("title",title[i]);
map.put("result",result[i]);
map.put("content",content[i]);
mSuggest.add(map);
}
return mSuggest;
}
@Override
public int getItemCount() {
return item_size;
}
//使用该方法根据position返回相应的viewtype
@Override
public int getItemViewType(int position) {
switch (position){
case 0:
return Variable.weather_item_type_1;
case 1:case 2:
return Variable.weather_item_type_2;
default:
return Variable.weather_item_type_3;
}
}
//为不同的布局创建不同的holder
class Weather_Holder extends RecyclerView.ViewHolder{
private TextView mCity,mTemp,mAir,mDate,mWeather,mtemperature,msuggest;
public Weather_Holder(View itemView) {
super(itemView);
mCity= (TextView) itemView.findViewById(R.id.city);
mTemp= (TextView) itemView.findViewById(R.id.temp);
mAir= (TextView) itemView.findViewById(R.id.air);
mDate= (TextView) itemView.findViewById(R.id.date);
mWeather= (TextView) itemView.findViewById(R.id.weather);
mtemperature= (TextView) itemView.findViewById(R.id.temperature);
msuggest= (TextView) itemView.findViewById(R.id.suggest);
}
}
class Day_Holder extends RecyclerView.ViewHolder{
private TextView mDate,mTemp,mWeather,mWind;
public Day_Holder(View itemView) {
super(itemView);
mDate= (TextView) itemView.findViewById(R.id.day_date);
mTemp= (TextView) itemView.findViewById(R.id.day_temp);
mWeather= (TextView) itemView.findViewById(R.id.day_weather);
mWind= (TextView) itemView.findViewById(R.id.day_wind);
}
}
class Live_Holder extends RecyclerView.ViewHolder{
private TextView mTitle,mResult,mContent;
public Live_Holder(View itemView) {
super(itemView);
mTitle= (TextView) itemView.findViewById(R.id.live_title);
mContent= (TextView) itemView.findViewById(R.id.live_content);
mResult= (TextView) itemView.findViewById(R.id.live_result);
}
}
//设置不同的viewtype所占用的列
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
Logger.d("onAttach");
RecyclerView.LayoutManager layout=recyclerView.getLayoutManager();//获取布局管理器对象
Logger.d(layout instanceof GridLayoutManager);
if (layout instanceof GridLayoutManager){
final GridLayoutManager manager= ((GridLayoutManager) recyclerView.getLayoutManager());
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int type=getItemViewType(position);//根据position获取item对应的viewtype
switch (type){
case Variable.weather_item_type_1:
return manager.getSpanCount();//返回总列数,表示该item独占一行
case Variable.weather_item_type_2:
Logger.t("adapter").d(manager.getSpanCount());
return manager.getSpanCount()/2;//表示item占一半的列
default:
return manager.getSpanCount();
}
}
});
}
}
}
- step3:在activity中使用recyclerview
mRecyclerView= (RecyclerView) findViewById(R.id.weather_list);
mAdapter=new Weather_Details_Adapter();
mAdapter.setContext(this);//向adapter中传递上下文
mAdapter.setWeather(weather);//向adapter中传递获取的天气数据
mRecyclerView.setLayoutManager(new GridLayoutManager(this,4, GridLayoutManager.VERTICAL,false));//设置布局管理器
mRecyclerView.setAdapter(mAdapter);