gpt4 book ai didi

android - 如何使用同一个适配器在回收站 View 上显示多个模型?

转载 作者:太空狗 更新时间:2023-10-29 16:26:13 25 4
gpt4 key购买 nike

我有两种不同类型的对象(例如:用户和学生),它们都具有不同的属性。

用户:标题、描述;
学生:标题、描述、USN;

有没有一种方法可以使用相同的适配器在回收站 View 中先显示两个用户,然后再显示学生?即使它是两个适配器,我如何显示它。我不想要两个回收站 View 。

请注意,给出的示例非常简单。更改不仅在 UI 中。如果更改仅在 UI 中,则可以使用 getItemViewType(),但此处更改在模型本身中。

List<Student>List<User>

我们如何将两个列表设置为相同的适配器?

最佳答案

您必须使用覆盖方法“getItemViewType(position)”。

然后像这样根据 ViewType 返回 ViewHolder,

  @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());

switch (viewType) {
case ITEM:
View v1 = inflater.inflate(R.layout.item_student, parent, false);
viewHolder = new UserVH(v1);
break;
case LOADING:
View v2 = inflater.inflate(R.layout.item_user, parent, false);
viewHolder = new StudentVH(v2);
break;
}
return viewHolder;
}

编辑:

 public class StudentVH extends RecyclerView.ViewHolder {
//Declare student views

public ViewHolder(View v) {
super(v);
//Inialize student views
}
}

public class UserVH extends RecyclerView.ViewHolder {
//Declare user views

public ViewHolder(View v) {
super(v);
//Inialize user views
}
}

第二次编辑:

你的 Adapter 应该是这样的,

public class ClGAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<UserBean> userBeans;
private List<StudentBean> studentBeans;
private UserBean userBeanItem;
private StudentBean studentBeanItem;
private int size = 0;


public ClGAdapter(Context context, List<UserBean> userBeans, List<StudentBean> studentBeans) {
this.context = context;
this.userBeans = userBeans;
this.studentBeans = studentBeans;
this.size = userBeans.size() + studentBeans.size();
}


@Override
public int getItemViewType(int position) {

if (position > (userBeans.size() - 1)) {
return 2;//TYPE_STUDENT
} else {
return 1; //TYPE_USER
}
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

int viewType = holder.getItemViewType();

userBeanItem = userBeans.get(position);
studentBeanItem = studentBeans.get(position);
switch (viewType) {
case 1:
((UserHolder) holder).mUserName.setText(userBeanItem.getUserName());
break;
case 2:
((StudentHolder) holder).mStudentName.setText(studentBeanItem.getStudentName());
break;
default:
break;
}

}

@Override
public int getItemCount() {
return size;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int layout = 0;
RecyclerView.ViewHolder viewHolder;
switch (viewType) {
case 1:
layout = R.layout.user_card;
View userView = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
viewHolder = new UserHolder(userView);
break;
case 2:
layout = R.layout.student_card;
View studentView = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
viewHolder = new StudentHolder(studentView);

break;
default:
viewHolder = null;
break;
}
return viewHolder;
}

public class UserHolder extends RecyclerView.ViewHolder {
private TextView mUserName;

public UserHolder(View v) {
super(v);
mUserName = v.findViewById(R.id.user_name);
}
}

public class StudentHolder extends RecyclerView.ViewHolder {
private TextView mStudentName;

public StudentHolder(View v) {
super(v);
mStudentName = v.findViewById(R.id.student_name);
}
}
}

关于android - 如何使用同一个适配器在回收站 View 上显示多个模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52252285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com