首先recyclerview适配器继承自RecyclerView.Adapter 不是之前的那个
public class MyDetailsAdapter extends RecyclerView.Adapter {
//设置常量
//平台
private static final int LEFT = 1;
//用户
private static final int RIGHT = 0;
//头部
private static final int TITLE = 3;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder holder = getViewHolderByViewType(viewType);
return holder;
}
/**
* 根据不同的position,设置不同的ViewType
* position表示当前是第几个Item,通过position拿到当前的Item对象,然后判断这个item对象需要那种视图
*/
@Override
public int getItemViewType(int position) {
if (myFeedbackDetailsModels.get(position).getSource().equals("1")) {
return LEFT;
} else if (myFeedbackDetailsModels.get(position).getSource().equals("0")) {
return RIGHT;
} else if (myFeedbackDetailsModels.get(position).getSource().equals("null")) {
return RIGHT;
} else if (myFeedbackDetailsModels.get(position).getSource().equals("3")) {
return TITLE;
}
return 0;
}
private RecyclerView.ViewHolder getViewHolderByViewType(int viewType) {
View left = View.inflate(mContext, R.layout.left_item, null);
View right = View.inflate(mContext, R.layout.right_item, null);
View title = View.inflate(mContext, R.layout.title_item, null);
RecyclerView.ViewHolder holder = null;
switch (viewType) {
case LEFT:
holder = new ViewHolderLeft(left);
break;
case RIGHT:
holder = new ViewHolderRight(right);
break;
case TITLE:
holder = new ViewHolderTitle(title);
break;
}
return holder;
}
//用于绑定数据
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//调用哪个布局就给那个布局绑定数据
if (holder instanceof ViewHolderLeft){
} else if (holder instanceof ViewHolderTitle) {
}else if (holder instanceof ViewHolderRight) {
}
}
//给每个布局加一个viewholder
private class ViewHolderLeft extends RecyclerView.ViewHolder {
TextView tv_date, tv_text, tv_no, tv_yes;
ImageView iv_head, iv_photo;
LinearLayout ll_solve;
public ViewHolderLeft(View itemView) {
super(itemView);
tv_date = itemView.findViewById(R.id.tv_date);
tv_text = itemView.findViewById(R.id.tv_text);
iv_head = itemView.findViewById(R.id.iv_head);
iv_photo = itemView.findViewById(R.id.iv_photo);
ll_solve = itemView.findViewById(R.id.ll_solve);
tv_no = itemView.findViewById(R.id.tv_no);
tv_yes = itemView.findViewById(R.id.tv_yes);
}
}
private class ViewHolderRight extends RecyclerView.ViewHolder {
TextView tv_date, tv_text;
ImageView iv_head;
ImageView iv_photo;
public ViewHolderRight(View itemView) {
super(itemView);
tv_date = itemView.findViewById(R.id.tv_date);
tv_text = itemView.findViewById(R.id.tv_text);
iv_head = itemView.findViewById(R.id.iv_head);
iv_photo = itemView.findViewById(R.id.iv_photo);
iv_head = itemView.findViewById(R.id.iv_head);
}
}
private class ViewHolderTitle extends RecyclerView.ViewHolder {
TextView tv_date, tv_text;
ImageView iv_head;
ImageView iv_img1, iv_img2, iv_img3, iv_more;
LinearLayout ll_img;
public ViewHolderTitle(View itemView) {
super(itemView);
tv_date = itemView.findViewById(R.id.tv_date);
tv_text = itemView.findViewById(R.id.tv_text);
iv_head = itemView.findViewById(R.id.iv_head);
//图片一
iv_img1 = itemView.findViewById(R.id.iv_img1);
//图片二
iv_img2 = itemView.findViewById(R.id.iv_img2);
//图片三
iv_img3 = itemView.findViewById(R.id.iv_img3);
//显示更多
iv_more = itemView.findViewById(R.id.iv_more);
//图片布局
ll_img = itemView.findViewById(R.id.ll_img);
}
}
来源:https://www.cnblogs.com/wang-jingyuan/p/12174058.html