|
|
|
@ -35,8 +35,10 @@ public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHold
|
|
|
|
|
private final Context context;
|
|
|
|
|
private final DataSetObserver observer = new AdapterDataSetObserver();
|
|
|
|
|
|
|
|
|
|
@VisibleForTesting final static int HEADER_TYPE = Integer.MIN_VALUE;
|
|
|
|
|
@VisibleForTesting final static int FOOTER_TYPE = Integer.MIN_VALUE + 1;
|
|
|
|
|
@VisibleForTesting static final int HEADER_TYPE = Integer.MIN_VALUE;
|
|
|
|
|
@VisibleForTesting static final int FOOTER_TYPE = Integer.MIN_VALUE + 1;
|
|
|
|
|
@VisibleForTesting static final long HEADER_ID = Long.MIN_VALUE;
|
|
|
|
|
@VisibleForTesting static final long FOOTER_ID = Long.MIN_VALUE + 1;
|
|
|
|
|
|
|
|
|
|
private Cursor cursor;
|
|
|
|
|
private boolean valid;
|
|
|
|
@ -143,8 +145,7 @@ public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHold
|
|
|
|
|
@Override
|
|
|
|
|
public final void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
|
|
|
|
|
if (!isHeaderPosition(position) && !isFooterPosition(position)) {
|
|
|
|
|
moveToPositionOrThrow(getCursorPosition(position));
|
|
|
|
|
onBindItemViewHolder((VH)viewHolder, cursor);
|
|
|
|
|
onBindItemViewHolder((VH)viewHolder, getCursorAtPositionOrThrow(position));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -154,8 +155,7 @@ public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHold
|
|
|
|
|
public final int getItemViewType(int position) {
|
|
|
|
|
if (isHeaderPosition(position)) return HEADER_TYPE;
|
|
|
|
|
if (isFooterPosition(position)) return FOOTER_TYPE;
|
|
|
|
|
moveToPositionOrThrow(getCursorPosition(position));
|
|
|
|
|
return getItemViewType(cursor);
|
|
|
|
|
return getItemViewType(getCursorAtPositionOrThrow(position));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getItemViewType(@NonNull Cursor cursor) {
|
|
|
|
@ -164,25 +164,24 @@ public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHold
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public final long getItemId(int position) {
|
|
|
|
|
moveToPositionOrThrow(getCursorPosition(position));
|
|
|
|
|
return getItemId(cursor);
|
|
|
|
|
if (isHeaderPosition(position)) return HEADER_ID;
|
|
|
|
|
if (isFooterPosition(position)) return FOOTER_ID;
|
|
|
|
|
long itemId = getItemId(getCursorAtPositionOrThrow(position));
|
|
|
|
|
return itemId <= Long.MIN_VALUE + 1 ? itemId + 2 : itemId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long getItemId(@NonNull Cursor cursor) {
|
|
|
|
|
return cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void assertActiveCursor() {
|
|
|
|
|
protected @NonNull Cursor getCursorAtPositionOrThrow(final int position) {
|
|
|
|
|
if (!isActiveCursor()) {
|
|
|
|
|
throw new IllegalStateException("this should only be called when the cursor is valid");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void moveToPositionOrThrow(final int position) {
|
|
|
|
|
assertActiveCursor();
|
|
|
|
|
if (!cursor.moveToPosition(position)) {
|
|
|
|
|
if (!cursor.moveToPosition(getCursorPosition(position))) {
|
|
|
|
|
throw new IllegalStateException("couldn't move cursor to position " + position);
|
|
|
|
|
}
|
|
|
|
|
return cursor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isActiveCursor() {
|
|
|
|
|