RecyclerView多條目介面卡
public class RecyclerAdapter extends RecyclerView.Adapter{
private List<MoreBean.ItemsBean> list=new ArrayList<>();
private Context context;
public RecyclerAdapter(Context mainActivity) {
context=mainActivity;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewtype) {
View view=null;
RecyclerView.ViewHolder viewHolder=null;
switch (viewtype){
case 0:
view=View.inflate(context,R.layout.recycler_item_0,null);
viewHolder=new MyViewHolder(view);
break;
case 1:
view=View.inflate(context,R.layout.recycler_item_1,null);
viewHolder=new MyViewHolder2(view);
break;
case 2:
view=View.inflate(context,R.layout.recycler_item_2,null);
viewHolder=new MyViewHolder3(view);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if(viewHolder instanceof MyViewHolder){
Glide.with(context).load(list.get(i).getImage()).into(((MyViewHolder)viewHolder).imageView);
//Picasso.with(context).load(list.get(i).getImage()).fit().into(((MyViewHolder)viewHolder).imageView);
((MyViewHolder)viewHolder).tvTitle.setText(list.get(i).getTitle());
((MyViewHolder)viewHolder).tvDesc.setText(list.get(i).getDesc());
}else if(viewHolder instanceof MyViewHolder2){
Glide.with(context).load(list.get(i).getImages().get(0).getPic()).into(((MyViewHolder2)viewHolder).image_1);
Glide.with(context).load(list.get(i).getImages().get(1).getPic()).into(((MyViewHolder2)viewHolder).image_2);
Glide.with(context).load(list.get(i).getImages().get(2).getPic()).into(((MyViewHolder2)viewHolder).image_3);
}else {
((MyViewHolder3)viewHolder).mTitle.setText(list.get(i).getTitle());
}
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public int getItemViewType(int position) {
return list.get(position).getType();
}
public void setList(List<MoreBean.ItemsBean> list) {
this.list = list;
notifyDataSetChanged();
}
private class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView tvTitle,tvDesc;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imageView=(ImageView) itemView.findViewById(R.id.image);
tvTitle=(TextView) itemView.findViewById(R.id.tv_title);
tvDesc=(TextView) itemView.findViewById(R.id.tv_desc);
}
}
private class MyViewHolder2 extends RecyclerView.ViewHolder{
ImageView image_1,image_2,image_3;
public MyViewHolder2(@NonNull View itemView) {
super(itemView);
image_1=(ImageView)itemView.findViewById(R.id.image_1);
image_2=(ImageView)itemView.findViewById(R.id.image_2);
image_3=(ImageView)itemView.findViewById(R.id.image_3);
}
}
private class MyViewHolder3 extends RecyclerView.ViewHolder{
TextView mTitle;
public MyViewHolder3(@NonNull View itemView) {
super(itemView);
mTitle=(TextView)itemView.findViewById(R.id.tv_title);
}
}
}