Created
August 7, 2016 06:16
-
-
Save yakuzaaaa/2aa174e6a03b676f764c944c8a0baa7c to your computer and use it in GitHub Desktop.
A cursor adapter implementation for recycler views in android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.database.Cursor; | |
import android.database.DataSetObserver; | |
import android.support.v7.widget.RecyclerView; | |
/** | |
* Created by nilarnab on 7/8/16. | |
*/ | |
public abstract class GenericRecyclerViewCursorAdapter<Vh extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<Vh> { | |
private Cursor mCursor; | |
private DataSetObserver mDataSetObserver; | |
public GenericRecyclerViewCursorAdapter(Cursor cursor) { | |
mCursor = cursor; | |
mDataSetObserver = new NotifyingDataSetObserver(); | |
if (mCursor != null) { | |
mCursor.registerDataSetObserver(mDataSetObserver); | |
} | |
setHasStableIds(true); | |
} | |
public Cursor getCursor() { | |
return mCursor; | |
} | |
@Override | |
public int getItemCount() { | |
return mCursor != null ? mCursor.getCount() : 0; | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
public abstract void onBindViewHolder(Vh viewHolder, Cursor cursor); | |
@Override | |
public void onBindViewHolder(Vh viewHolder, int position) { | |
if (mCursor == null || !mCursor.moveToPosition(position)) { | |
throw new IllegalStateException("couldn't move cursor to position " + position); | |
} | |
onBindViewHolder(viewHolder, mCursor); | |
} | |
public Cursor swapCursor(Cursor newCursor) { | |
if (newCursor == mCursor) { | |
return null; | |
} | |
final Cursor oldCursor = mCursor; | |
if (oldCursor != null && mDataSetObserver != null) { | |
oldCursor.unregisterDataSetObserver(mDataSetObserver); | |
} | |
mCursor = newCursor; | |
if (mCursor != null) { | |
if (mDataSetObserver != null) { | |
mCursor.registerDataSetObserver(mDataSetObserver); | |
} | |
notifyDataSetChanged(); | |
} else { | |
notifyDataSetChanged(); | |
//There is no notifyDataSetInvalidated() method in RecyclerView.Adapter | |
} | |
return oldCursor; | |
} | |
private class NotifyingDataSetObserver extends DataSetObserver { | |
@Override | |
public void onChanged() { | |
super.onChanged(); | |
notifyDataSetChanged(); | |
} | |
@Override | |
public void onInvalidated() { | |
super.onInvalidated(); | |
notifyDataSetChanged(); | |
//There is no notifyDataSetInvalidated() method in RecyclerView.Adapter | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment