码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-01-03 14:08:41      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:parent   集合   文字   ima   rom   一个   adapter   json   inflate   

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值

标签:parent   集合   文字   ima   rom   一个   adapter   json   inflate   

原文地址:https://www.cnblogs.com/ggjy/p/8183068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!