Recycler多布局(适配器)之有Type值

Posted ggjy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Recycler多布局(适配器)之有Type值相关的知识,希望对你有一定的参考价值。

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;
}

}

//这是一个简易的多布局的适配器,若有不完全,请留下评语,我会加以改进,谢谢!!!

以上是关于Recycler多布局(适配器)之有Type值的主要内容,如果未能解决你的问题,请参考以下文章

Android recycler view onItemClick 识别适配器

RecycleView的多布局

Recycler View项目onClick无法正常工作。(工作异常)

recyclerview的使用

在Recycler视图中使用数组列表

BaseQuickAdapter万能适配器--分类型实现多布局效果的3个适配器