How to display multiple models on recycler view using same adapater?

岁酱吖の 提交于 2019-12-12 21:22:43

问题


I have two different types of objects (ex: User and Student) both having different properties.

User: Title, description; Student: Title, description, USN;

Is there a way using which I can display two users first and then students on the recycler view using same adapter? How do I display it even if it's two adapters. I dont' want two recycler views.

Please note that the example given is very simple. The change is not just in UI. If the change is just in UI, then getItemViewType() can be used but here the change is in the model itself.

List and List

How do we set both the lists to same adapter??


回答1:


You have to use the override method "getItemViewType(position)".

Then just return the ViewHolder as per the ViewType like this,

  @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;
}

Edit:

 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
    }
}

Second Edit:

Your Adapter should be like this,

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);
    }
}
}



回答2:


just use viewType, return different viewType and bind different data in onBindViewHolder




回答3:


I will give you my implementation, hope will help:

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;
    }

}


来源:https://stackoverflow.com/questions/52252285/how-to-display-multiple-models-on-recycler-view-using-same-adapater

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!