fix header/footer item ID issue

fixes #4667
Closes #4685
// FREEBIE
This commit is contained in:
Jake McGinty 2015-11-22 16:04:49 -08:00 committed by Moxie Marlinspike
parent 4dce79334d
commit 473e470883
2 changed files with 13 additions and 25 deletions

View file

@ -178,17 +178,6 @@ public class ContactSelectionListAdapter extends CursorRecyclerViewAdapter<ViewH
return cursor.getInt(cursor.getColumnIndexOrThrow(ContactsDatabase.CONTACT_TYPE_COLUMN)) == ContactsDatabase.PUSH_TYPE;
}
private Cursor getCursorAtPositionOrThrow(int position) {
Cursor cursor = getCursor();
if (cursor == null) {
throw new IllegalStateException("Cursor should not be null here.");
}
if (!cursor.moveToPosition(position)) {
throw new IllegalStateException("Cursor couldn't be moved to position.");
}
return cursor;
}
public interface ItemClickListener {
void onItemClick(ContactSelectionListItem item);
}

View file

@ -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() {