我有两种具有不同属性的不同类型的对象(例如:User和Student)。
用户: 标题描述; 学生: 标题,描述,USN;
有没有一种方法可以使用相同的适配器先显示两个用户,然后在回收者视图中显示学生?即使是两个适配器,如何显示它。我不希望有两个回收站视图。
请注意,给出的示例非常简单。更改不只是在UI中。如果只是在UI中进行更改,则可以使用getItemViewType(),但此处的更改在模型本身中。
列表和列表
我们如何将两个列表都设置为同一适配器?
答案 0 :(得分:4)
您必须使用覆盖方法“ 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
}
}
第二次修改:
您的适配器应该是这样的,
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);
}
}
}
答案 1 :(得分:0)
只使用viewType,返回不同的viewType并在onBindViewHolder中绑定不同的数据
答案 2 :(得分:0)
我会给您我的实施方案,希望会对您有所帮助:
public class UserStudentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int USER_VIEW_TYPE = 0;
private static final int STUDENT_VIEW_TYPE = 1;
private List<User> mUserData;
private List<Student> mStudentData;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case HEADER_VIEW_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_definition_header, parent, false);
return new HeaderViewHolder(view);
case CONTENT_VIEW_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index, parent, false);
return new ItemViewHolder(view);
default:
return null;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case USER_VIEW_TYPE:
//Show User data
break;
case STUDENT_VIEW_TYPE:
//Show Student data
break;
default:
break;
}
}
@Override
public int getItemCount() {
return mUserData.size() + mStudentData.size();
}
@Override
public int getItemViewType(int position) {
return (position % 2 == 0) ? STUDENT_VIEW_TYPE : USER_VIEW_TYPE;
}
}