RecyclerView多布局简易适配器
public class MyDListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<JsonBean> al;//集合
private Context mC;//上下文对象
public MyDListAdapter(List<JsonBean> al, Context mC) {
this.al = al;
this.mC = mC;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder holder = null;
switch (viewType) {
case 0:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list1_item, parent, false);
holder = new MyOneHolder(view);
break;
case 1:
View view1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.list2_item, parent, false);
holder = new MyTwoHolder(view1);
break;
}
return holder;
}
//当有Type时,你会发现报错,愿意是这里是Int类型的,请将实体类中的String类型的改为int类型就行了
@Override
public int getItemViewType(int position) {
return al.get(position).getType();
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
int jsonBean = al.get(position).getType();
switch (jsonBean) {
case 0:
MyOneHolder holder1 = (MyOneHolder) holder;
//文字
((MyOneHolder) holder1).item1_title.setText(al.get(position).getTitle());
//图片
JsonBean.PicBean picBean1 = pic.get(1);
Picasso.with(mC).load(picBean1.getThumbnail_pic()).into(((MyOneHolder) holder).item1_pic1);
//接口回调
((MyOneHolder) holder1).linear1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inter1.setResult1(position);
}
});
break;
case 1:
final MyTwoHolder holder2 = (MyTwoHolder) holder;
//文字
((MyTwoHolder) holder2).item2_title.setText(al.get(position).getTitle());
//图片
List<JsonBean.PicBean> pic1 = al.get(position).getPic();
JsonBean.PicBean thumbnail_pic1 = pic1.get(0);
//接口回调
((MyTwoHolder) holder2).linear2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inter2.setResult2(position);
}
});
break;
}
}
@Override
public int getItemCount() {
return al.isEmpty() ? 0 : al.size();
}
class MyOneHolder extends RecyclerView.ViewHolder {
private LinearLayout linear1;
private TextView item1_title;
private ImageView item1_pic1;
public MyOneHolder(View itemView) {
super(itemView);
item1_title = itemView.findViewById(R.id.item1_title);
item1_pic1 = itemView.findViewById(R.id.item1_pic1);
linear1 = itemView.findViewById(R.id.linear1);
}
}
class MyTwoHolder extends RecyclerView.ViewHolder {
private LinearLayout linear2;
private TextView item2_title;
private ImageView item2_pic;
public MyTwoHolder(View itemView) {
super(itemView);
item2_title = itemView.findViewById(R.id.item2_title);
item2_pic = itemView.findViewById(R.id.item2_pic);
linear2 = itemView.findViewById(R.id.linear2);
}
}
//以下是个接口回调
private MyInter1 inter1;
public interface MyInter1 {
void setResult1(int position1);
}
public void setOnclickLister1(MyInter1 inter1) {
this.inter1 = inter1;
}
private MyInter2 inter2;
public interface MyInter2 {
void setResult2(int position2);
}
public void setOnclickLister2(MyInter2 inter2) {
this.inter2 = inter2;
}
}
//这是一个简易的多布局的适配器,若有不完全,请留下评语,我会加以改进,谢谢!!!