public static int unwrapPosition(@Nullable RecyclerView.Adapter originAdapter, @Nullable RecyclerView.Adapter targetAdapter, @Nullable Object targetAdapterTag, int originPosition, @Nullable AdapterPath destPath) { RecyclerView.Adapter wrapper = originAdapter; int wrappedPosition = originPosition; final UnwrapPositionResult tmpResult = new UnwrapPositionResult(); Object wrappedAdapterTag = null; tmpResult.clear(); wrapperParentAdapter.unwrapPosition(tmpResult, wrappedPosition); wrappedPosition = tmpResult.position; wrappedAdapterTag = tmpResult.tag; if (tmpResult.isValid()) { if (destPath != null) { destPath.append(tmpResult);
/** * {@inheritDoc} * <p> * This class also invokes {@link WrapperAdapter#wrapPosition(AdapterPathSegment, int)} of the child adapter when * verify option is enabled. * If inconsistency has been detected, <code>IllegalStateException</code> will be thrown. */ @Override public void unwrapPosition(@NonNull UnwrapPositionResult dest, int position) { if (((mFlags & FLAG_VERIFY_UNWRAP_POSITION) != 0) && (getWrappedAdapter() instanceof WrapperAdapter)) { final WrapperAdapter wrapperAdapter = (WrapperAdapter) getWrappedAdapter(); final UnwrapPositionResult tmpResult = new UnwrapPositionResult(); wrapperAdapter.unwrapPosition(tmpResult, position); if (tmpResult.isValid()) { final AdapterPathSegment segment = new AdapterPathSegment(tmpResult.adapter, tmpResult.tag); final int reWrappedPosition = wrapperAdapter.wrapPosition(segment, tmpResult.position); if (position != reWrappedPosition) { final String wrappedClassName = getWrappedAdapter().getClass().getSimpleName(); throw new IllegalStateException( "Found a WrapperAdapter implementation issue while executing unwrapPosition(): " + wrappedClassName + "\n" + "unwrapPosition(" + position + ") returns " + tmpResult.position + ", but " + "wrapPosition(" + tmpResult.position + ") returns " + reWrappedPosition); } } } super.unwrapPosition(dest, position); }
/** * {@inheritDoc} * <p> * This class also invokes {@link WrapperAdapter#unwrapPosition(UnwrapPositionResult, int)} of the child adapter when * verify option is enabled. * If inconsistency has been detected, <code>IllegalStateException</code> will be thrown. */ @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); }
@Test public void unwrapPosition() 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); assertThat(result.isValid(), is(true)); assertThat(result.adapter, is(adapter)); assertThat(result.position, is(i)); } }
@Test public void unwrapPosition_withBadAdapter() throws Exception { RecyclerView.Adapter adapter = new BadUnwrapPositionAdapter(new TestAdapter()); DebugWrapperAdapter debugAdapter = new DebugWrapperAdapter(adapter); UnwrapPositionResult result = new UnwrapPositionResult(); thrown.expect(IllegalStateException.class); debugAdapter.unwrapPosition(result, 0); }
@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)); } }
@Test public void wrapPosition_withBadAdapter() throws Exception { RecyclerView.Adapter adapter = new BadWrapPositionAdapter(new TestAdapter()); DebugWrapperAdapter debugAdapter = new DebugWrapperAdapter(adapter); debugAdapter.setSettingFlags(0); UnwrapPositionResult result = new UnwrapPositionResult(); debugAdapter.unwrapPosition(result, 0); AdapterPath path = new AdapterPath().append(result); debugAdapter.setSettingFlags(DebugWrapperAdapter.FLAG_VERIFY_WRAP_POSITION); thrown.expect(IllegalStateException.class); debugAdapter.wrapPosition(path.lastSegment(), result.position); }