Skip to content

Instantly share code, notes, and snippets.

@renaudcerrato
Last active November 7, 2017 20:19
Show Gist options
  • Save renaudcerrato/61b3b0c9844fa47ae3d8 to your computer and use it in GitHub Desktop.
Save renaudcerrato/61b3b0c9844fa47ae3d8 to your computer and use it in GitHub Desktop.
Simple wrapper for RecyclerAdapter.
package recyclerview.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
@SuppressWarnings("unchecked")
public class WrapperRecyclerAdapter extends RecyclerView.Adapter {
private final RecyclerView.Adapter mAdapter;
public WrapperRecyclerAdapter(RecyclerView.Adapter adapter) {
super.setHasStableIds(adapter.hasStableIds());
mAdapter = adapter;
mAdapter.registerAdapterDataObserver(new ForwardingDataSetObserver());
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return mAdapter.onCreateViewHolder(parent, viewType);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
mAdapter.onBindViewHolder(holder, position);
}
@Override
public int getItemViewType(int position) {
return mAdapter.getItemViewType(position);
}
@Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
mAdapter.onViewRecycled(holder);
}
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
mAdapter.onViewAttachedToWindow(holder);
}
@Override
public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
mAdapter.onViewDetachedFromWindow(holder);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
mAdapter.onAttachedToRecyclerView(recyclerView);
}
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
mAdapter.onDetachedFromRecyclerView(recyclerView);
}
@Override
public long getItemId(int position) {
return mAdapter.getItemId(position);
}
@Override
public int getItemCount() {
return mAdapter.getItemCount();
}
private class ForwardingDataSetObserver extends RecyclerView.AdapterDataObserver {
@Override public void onChanged() {
notifyDataSetChanged();
}
@Override public void onItemRangeChanged(int positionStart, int itemCount) {
notifyItemRangeChanged(positionStart, itemCount);
}
@Override public void onItemRangeInserted(int positionStart, int itemCount) {
notifyItemRangeInserted(positionStart, itemCount);
}
@Override public void onItemRangeRemoved(int positionStart, int itemCount) {
notifyItemRangeRemoved(positionStart, itemCount);
}
@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
notifyDataSetChanged(); //TODO
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment