z85510 z85510 - 1 year ago 67
Android Question

How combine two type of Adapter in List?

I want to create a List of 2 type of struct.

How can I combine two type of adapter in one list like this ?

Adapter 1 create base on struct 1 and Adapter 2 create base on struct 2.

Three type of list

Answer Source

You can display severel types of listview row layout in your RecyclerView - you dont need multiple adapters

You need to override the getItemCount and the getItemViewType methods of the recycler view and tell it how much types you want to display in the list look at this: RecyclerView item count

Here is an example:

the adapter:

public class NotificationsAdapter extends RecyclerView.Adapter<MultipleRowViewHolder> {

private LayoutInflater inflater = null;

// notification list
private List<Notification> notificationList;

public NotificationsAdapter(Context context, List<Notification> multipleRowModelList){
    this.notificationList = multipleRowModelList;
    inflater = LayoutInflater.from(context);

public MultipleRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = null;

    if (viewType == NotificationType.Lick.getValue())
        view = inflater.inflate(R.layout.lick_row_item, parent, false);
    else if (viewType == NotificationType.Comment.getValue())
        view = inflater.inflate(R.layout.comment_list_row, parent, false);
    else if (viewType == NotificationType.ViewMyProfile.getValue())
        view = inflater.inflate(R.layout.view_my_profile_list_row, parent, false);
    else // if notification is not of the first three types get the view_my_profile_list_row layout
        view = inflater.inflate(R.layout.view_my_profile_list_row, parent, false);

    return new MultipleRowViewHolder(view, viewType);
public void onBindViewHolder(MultipleRowViewHolder holder, int position) {

    try {
        // display enum type as title
        // display pretty date
    }catch (Exception e)
public int getItemCount() {
    return notificationList.size();

public int getItemViewType(int position) {

    Notification multipleRowModel = notificationList.get(position);

    if (multipleRowModel != null)
        return Integer.valueOf(multipleRowModel.getType());

    return super.getItemViewType(position);

The data model:

public class MultipleRowViewHolder extends RecyclerView.ViewHolder {

public TextView titleTV;
public TextView sourceUserNameTV;
public TextView actionTimeTV;

private int type;

public MultipleRowViewHolder(View itemView, int type) {

    titleTV = (TextView)itemView.findViewById(R.id.titleTV);
    sourceUserNameTV = (TextView)itemView.findViewById(R.id.sourceUserNameTV);
    actionTimeTV = (TextView)itemView.findViewById(R.id.actionTimeTV);

    this.type = type;



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download