@Test public void wrapPosition() throws Exception { RecyclerView.Adapter adapter = new GoodWrapperAdapter(new TestAdapter()); DebugWrapperAdapter debugAdapter = new DebugWrapperAdapter(adapter); for (int i = 0; i < adapter.getItemCount(); i++) { UnwrapPositionResult result = new UnwrapPositionResult(); debugAdapter.unwrapPosition(result, i); AdapterPath path = new AdapterPath().append(result); int wrappedPosition = debugAdapter.wrapPosition(path.lastSegment(), result.position); assertThat(wrappedPosition, is(i)); } }
@Override public int wrapPosition(@NonNull AdapterPathSegment pathSegment, int position) { if (((mFlags & FLAG_VERIFY_WRAP_POSITION) != 0) && (getWrappedAdapter() instanceof WrapperAdapter)) { final WrapperAdapter wrapperAdapter = (WrapperAdapter) getWrappedAdapter(); final int wrappedPosition = wrapperAdapter.wrapPosition(pathSegment, position); if (wrappedPosition != RecyclerView.NO_POSITION) { final UnwrapPositionResult tmpResult = new UnwrapPositionResult(); wrapperAdapter.unwrapPosition(tmpResult, wrappedPosition); if (tmpResult.position != position) { final String wrappedClassName = getWrappedAdapter().getClass().getSimpleName(); throw new IllegalStateException( "Found a WrapperAdapter implementation issue while executing wrapPosition(): " + wrappedClassName + "\n" + "wrapPosition(" + position + ") returns " + wrappedPosition + ", but " + "unwrapPosition(" + wrappedPosition + ") returns " + tmpResult.position); } } } return super.wrapPosition(pathSegment, position); }
DebugWrapperAdapter extends SimpleWrapperAdapter<RecyclerView.ViewHolder> { @Override public int wrapPosition(@NonNull AdapterPathSegment pathSegment, int position) { if (((mFlags & FLAG_VERIFY_WRAP_POSITION) != 0) && (getWrappedAdapter() instanceof WrapperAdapter)) { final WrapperAdapter wrapperAdapter = (WrapperAdapter) getWrappedAdapter(); final int wrappedPosition = wrapperAdapter.wrapPosition(pathSegment, position); if (wrappedPosition != RecyclerView.NO_POSITION) { final UnwrapPositionResult tmpResult = new UnwrapPositionResult(); wrapperAdapter.unwrapPosition(tmpResult, wrappedPosition); if (tmpResult.position != position) { final String wrappedClassName = getWrappedAdapter().getClass().getSimpleName(); throw new IllegalStateException( "Found a WrapperAdapter implementation issue while executing wrapPosition(): " + wrappedClassName + "\n" + "wrapPosition(" + position + ") returns " + wrappedPosition + ", but " + "unwrapPosition(" + wrappedPosition + ") returns " + tmpResult.position); } } } return super.wrapPosition(pathSegment, position); } }
DebugWrapperAdapter extends SimpleWrapperAdapter<RecyclerView.ViewHolder> { @Override public int wrapPosition(@NonNull AdapterPathSegment pathSegment, int position) { if (((mFlags & FLAG_VERIFY_WRAP_POSITION) != 0) && (getWrappedAdapter() instanceof WrapperAdapter)) { final WrapperAdapter wrapperAdapter = (WrapperAdapter) getWrappedAdapter(); final int wrappedPosition = wrapperAdapter.wrapPosition(pathSegment, position); if (wrappedPosition != RecyclerView.NO_POSITION) { final UnwrapPositionResult tmpResult = new UnwrapPositionResult(); wrapperAdapter.unwrapPosition(tmpResult, wrappedPosition); if (tmpResult.position != position) { final String wrappedClassName = getWrappedAdapter().getClass().getSimpleName(); throw new IllegalStateException( "Found a WrapperAdapter implementation issue while executing wrapPosition(): " + wrappedClassName + "\n" + "wrapPosition(" + position + ") returns " + wrappedPosition + ", but " + "unwrapPosition(" + wrappedPosition + ") returns " + tmpResult.position); } } } return super.wrapPosition(pathSegment, position); } @SuppressWarnings("unchecked")  DebugWrapperAdapter(@NonNull RecyclerView.Adapter adapter); }
DebugWrapperAdapter extends SimpleWrapperAdapter<RecyclerView.ViewHolder> { @Override public int wrapPosition(@NonNull AdapterPathSegment pathSegment, int position) { if (((mFlags & FLAG_VERIFY_WRAP_POSITION) != 0) && (getWrappedAdapter() instanceof WrapperAdapter)) { final WrapperAdapter wrapperAdapter = (WrapperAdapter) getWrappedAdapter(); final int wrappedPosition = wrapperAdapter.wrapPosition(pathSegment, position); if (wrappedPosition != RecyclerView.NO_POSITION) { final UnwrapPositionResult tmpResult = new UnwrapPositionResult(); wrapperAdapter.unwrapPosition(tmpResult, wrappedPosition); if (tmpResult.position != position) { final String wrappedClassName = getWrappedAdapter().getClass().getSimpleName(); throw new IllegalStateException( "Found a WrapperAdapter implementation issue while executing wrapPosition(): " + wrappedClassName + "\n" + "wrapPosition(" + position + ") returns " + wrappedPosition + ", but " + "unwrapPosition(" + wrappedPosition + ") returns " + tmpResult.position); } } } return super.wrapPosition(pathSegment, position); } @SuppressWarnings("unchecked")  DebugWrapperAdapter(@NonNull RecyclerView.Adapter adapter); @Override int wrapPosition(@NonNull AdapterPathSegment pathSegment, int position); @Override void unwrapPosition(@NonNull UnwrapPositionResult dest, int position); @DebugWrapperAdapterSettingFlags int getSettingFlags(); void setSettingFlags(@DebugWrapperAdapterSettingFlags int flags); }
DebugWrapperAdapter extends SimpleWrapperAdapter<RecyclerView.ViewHolder> { @Override public int wrapPosition(@NonNull AdapterPathSegment pathSegment, int position) { if (((mFlags & FLAG_VERIFY_WRAP_POSITION) != 0) && (getWrappedAdapter() instanceof WrapperAdapter)) { final WrapperAdapter wrapperAdapter = (WrapperAdapter) getWrappedAdapter(); final int wrappedPosition = wrapperAdapter.wrapPosition(pathSegment, position); if (wrappedPosition != RecyclerView.NO_POSITION) { final UnwrapPositionResult tmpResult = new UnwrapPositionResult(); wrapperAdapter.unwrapPosition(tmpResult, wrappedPosition); if (tmpResult.position != position) { final String wrappedClassName = getWrappedAdapter().getClass().getSimpleName(); throw new IllegalStateException( "Found a WrapperAdapter implementation issue while executing wrapPosition(): " + wrappedClassName + "\n" + "wrapPosition(" + position + ") returns " + wrappedPosition + ", but " + "unwrapPosition(" + wrappedPosition + ") returns " + tmpResult.position); } } } return super.wrapPosition(pathSegment, position); } @SuppressWarnings("unchecked")  DebugWrapperAdapter(@NonNull RecyclerView.Adapter adapter); @Override int wrapPosition(@NonNull AdapterPathSegment pathSegment, int position); @Override void unwrapPosition(@NonNull UnwrapPositionResult dest, int position); @DebugWrapperAdapterSettingFlags int getSettingFlags(); void setSettingFlags(@DebugWrapperAdapterSettingFlags int flags); static final int FLAG_VERIFY_WRAP_POSITION; static final int FLAG_VERIFY_UNWRAP_POSITION; static final int FLAGS_ALL_DEBUG_FEATURES; }