我有两种不同类型的对象(例如:用户和学生)都具有不同的属性。
用户:标题,描述;学生:标题,描述,USN;
有没有一种方法可以让我首先显示两个用户,然后使用同一个适配器在回收者视图上显示学生?即使它是两个适配器,我该如何显示它。我不想要两个回收者的观点。
请注意,给出的示例非常简单。这种变化不只是在UI中。如果更改只是在UI中,则可以使用getItemViewType(),但此处更改位于模型本身中。
列表和列表
我们如何将两个列表设置为相同的适配器?
您必须使用覆盖方法“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);
}
}
}
只需使用viewType,返回不同的viewType并绑定onBindViewHolder中的不同数据
我会给你我的实施,希望有所帮助:
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;
}
}