Skip to content

Instantly share code, notes, and snippets.

@lvsecoto
Last active October 6, 2020 15:40
Show Gist options
  • Save lvsecoto/e8d6cd134c3941e793f135cb5c83c887 to your computer and use it in GitHub Desktop.
Save lvsecoto/e8d6cd134c3941e793f135cb5c83c887 to your computer and use it in GitHub Desktop.
The is recyclerview adapter is use for concat with two adapter, by decorating
package com.yjy.recyclerviewutils.adapter;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.ViewGroup;
public class ConcatAdapter extends RecyclerView.Adapter {
private RecyclerView.Adapter mFirstAdapter;
private RecyclerView.Adapter mLastAdapter;
private SparseArray<RecyclerView.Adapter> mViewTypeAdapterMap = new SparseArray<>();
public ConcatAdapter(RecyclerView.Adapter firstAdapter, RecyclerView.Adapter lastAdapter) {
mFirstAdapter = firstAdapter;
mLastAdapter = lastAdapter;
}
@Override
public int getItemViewType(int position) {
int itemViewType;
if (position < mFirstAdapter.getItemCount()) {
itemViewType = mFirstAdapter.getItemViewType(position);
mViewTypeAdapterMap.put(itemViewType, mFirstAdapter);
} else {
itemViewType = mLastAdapter.getItemViewType(position);
mViewTypeAdapterMap.put(itemViewType, mLastAdapter);
}
return itemViewType;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return mViewTypeAdapterMap.get(viewType).onCreateViewHolder(parent, viewType);
}
@SuppressWarnings("unchecked")
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (position < mFirstAdapter.getItemCount()) {
mFirstAdapter.onBindViewHolder(holder, position);
} else {
mLastAdapter.onBindViewHolder(holder, position);
}
}
@Override
public int getItemCount() {
return mFirstAdapter.getItemCount() + mLastAdapter.getItemCount();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment