/** * {@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); }
wrappedAdapterTag = tmpResult.tag; if (tmpResult.isValid()) { if (destPath != null) { destPath.append(tmpResult);
@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)); } }