androidx.recyclerview.widget.OrientationHelper Java Examples

The following examples show how to use androidx.recyclerview.widget.OrientationHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GravitySnapHelper.java    From GravitySnapHelper with Apache License 2.0 6 votes vote down vote up
private int getDistanceToEnd(View targetView, @NonNull OrientationHelper helper) {
    int distance;

    if (!snapToPadding) {
        int childEnd = helper.getDecoratedEnd(targetView);
        if (childEnd >= helper.getEnd() - (helper.getEnd() - helper.getEndAfterPadding()) / 2) {
            distance = helper.getDecoratedEnd(targetView) - helper.getEnd();
        } else {
            distance = childEnd - helper.getEndAfterPadding();
        }
    } else {
        distance = helper.getDecoratedEnd(targetView) - helper.getEndAfterPadding();
    }

    return distance;
}
 
Example #2
Source File: CenteringRecyclerView.java    From centering-recycler-view with Apache License 2.0 6 votes vote down vote up
/**
 * Calculates and returns the center offset size.
 *
 * @param orientation   The layout orientation.
 * @param childPosition The visible child position.
 * @return the center offset or the last known offset if a child at the position is null.
 */
private int getCenterOffset(int orientation, int childPosition) {
    View child = getChildAt(childPosition);
    if (child == null) {
        return mFallbackCenterOffset;
    }

    final Rect r = new Rect();
    if (getGlobalVisibleRect(r)) {
        if (orientation == OrientationHelper.HORIZONTAL) {
            mFallbackCenterOffset = r.width() / 2 - child.getWidth() / 2;
        } else {
            mFallbackCenterOffset = r.height() / 2 - child.getHeight() / 2;
        }
    } else {
        if (orientation == OrientationHelper.HORIZONTAL) {
            mFallbackCenterOffset = getWidth() / 2 - child.getWidth() / 2;
        } else {
            mFallbackCenterOffset = getHeight() / 2 - child.getHeight() / 2;
        }
    }

    return mFallbackCenterOffset;
}
 
Example #3
Source File: CarouselSnapHelper.java    From CarouselView with MIT License 6 votes vote down vote up
private View findFirstView(RecyclerView.LayoutManager layoutManager, OrientationHelper helper) {
  if (layoutManager == null) return null;

  int childCount = layoutManager.getChildCount();
  if (childCount == 0) return null;

  int absClosest = Integer.MAX_VALUE;
  View closestView = null;
  int start = helper.getStartAfterPadding();

  for (int i=0; i < childCount; i++) {
    View child = layoutManager.getChildAt(i);
    int childStart = helper.getDecoratedStart(child);
    int absDistanceToStart = Math.abs(childStart - start);
    if (absDistanceToStart < absClosest) {
      absClosest = absDistanceToStart;
      closestView = child;
    }
  }

  return closestView;
}
 
Example #4
Source File: ImageActivity.java    From cloudinary_android with MIT License 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setVisibility(View.GONE);
    imageView = (ImageView) findViewById(R.id.image_view);
    descriptionTextView = (TextView) findViewById(R.id.effectDescription);
    recyclerView = (RecyclerView) findViewById(R.id.effectsGallery);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this, OrientationHelper.HORIZONTAL, false);
    recyclerView.setLayoutManager(layoutManager);

    initExoPlayer();

    fetchImageFromIntent(getIntent());
}
 
Example #5
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrapContentWithRemoveOnVertical() {
  final int widthSpec = makeSizeSpec(1000, EXACTLY);
  final int heightSpec = makeSizeSpec(1000, AT_MOST);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.VERTICAL, 100);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeItemAt(0);
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.height).isEqualTo(700);
}
 
Example #6
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Ignore("t33888191") // TODO(t33888191): Support wrapContent with insertAsync
@Test
public void testWrapContentWithRemoveAsyncOnVertical() {
  final int widthSpec = makeSizeSpec(1000, EXACTLY);
  final int heightSpec = makeSizeSpec(1000, AT_MOST);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.VERTICAL, 100, true);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeItemAtAsync(0);
  recyclerBinder.notifyChangeSetCompleteAsync(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  mLayoutThreadShadowLooper.runToEndOfTasks();

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.height).isEqualTo(700);
}
 
Example #7
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Ignore("t33888191") // TODO(t33888191): Support wrapContent with insertAsync
@Test
public void testWrapContentWithRemoveRangeAsyncOnVertical() {
  final int widthSpec = makeSizeSpec(1000, EXACTLY);
  final int heightSpec = makeSizeSpec(1000, AT_MOST);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.VERTICAL, 100, true);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeRangeAtAsync(0, 3);
  recyclerBinder.notifyChangeSetCompleteAsync(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  mLayoutThreadShadowLooper.runToEndOfTasks();

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.height).isEqualTo(500);
}
 
Example #8
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrapContentWithRemoveRangeOnHorizontal() {
  final int widthSpec = makeSizeSpec(1000, AT_MOST);
  final int heightSpec = makeSizeSpec(1000, EXACTLY);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.HORIZONTAL, 100);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeRangeAt(0, 3);
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.width).isEqualTo(500);
}
 
Example #9
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Ignore("t33888191") // TODO(t33888191): Support wrapContent with insertAsync
@Test
public void testWrapContentWithRemoveAsyncOnHorizontal() {
  final int widthSpec = makeSizeSpec(1000, AT_MOST);
  final int heightSpec = makeSizeSpec(1000, EXACTLY);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.HORIZONTAL, 100, true);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeItemAt(0);
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  mLayoutThreadShadowLooper.runToEndOfTasks();

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.width).isEqualTo(700);
}
 
Example #10
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Ignore("t33888191") // TODO(t33888191): Support wrapContent with insertAsync
@Test
public void testWrapContentWithRemoveRangeAsyncOnHorizontal() {
  final int widthSpec = makeSizeSpec(1000, AT_MOST);
  final int heightSpec = makeSizeSpec(1000, EXACTLY);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.HORIZONTAL, 100, true);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeRangeAt(0, 3);
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  mLayoutThreadShadowLooper.runToEndOfTasks();

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.width).isEqualTo(500);
}
 
Example #11
Source File: BrickFragment.java    From brickkit-android with Apache License 2.0 6 votes vote down vote up
@Override
@CallSuper
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view;
    if (orientation() == OrientationHelper.VERTICAL) {
        view = inflater.inflate(R.layout.vertical_fragment_brick, container, false);
    } else {
        view = inflater.inflate(R.layout.horizontal_fragment_brick, container, false);
    }

    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    recyclerView.setBackgroundColor(recyclerViewBackground);
    ((DefaultItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
    dataManager.setRecyclerView(getContext(), recyclerView, orientation(), reverse(), view);
    return view;
}
 
Example #12
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 6 votes vote down vote up
/**
 * 水平填充布局测量
 *
 * @param recycler RecyclerView.Recycler
 * @param offset   水平偏移量
 */
private void fillWithHorizontal(RecyclerView.Recycler recycler, int offset) {
    final OrientationHelper orientationHelper = getOrientationHelper();
    final int leftEdge = orientationHelper.getStartAfterPadding();
    final int rightEdge = orientationHelper.getEndAfterPadding();
    if (getChildCount() > 0) {
        if (offset >= 0) {
            removeAndRecyclerWithLeft(recycler, leftEdge + offset);
        } else {
            removeAndRecyclerWithRight(recycler, rightEdge + offset);
        }

    }
    if (offset >= 0) {
        // 右滑
        fillRight(recycler, rightEdge + offset);
    } else {
        // 左滑
        fillLeft(recycler, leftEdge + offset);
    }
}
 
Example #13
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 6 votes vote down vote up
/**
 * 垂直填充布局测量
 *
 * @param recycler RecyclerView.Recycler
 * @param offset   垂直偏移量
 */
private void fillWithVertical(RecyclerView.Recycler recycler, int offset) {
    final OrientationHelper orientationHelper = getOrientationHelper();
    final int topEdge = orientationHelper.getStartAfterPadding();
    final int bottomEdge = orientationHelper.getEndAfterPadding();
    if (getChildCount() > 0) {
        if (offset >= 0) {
            // 下滑
            removeAndRecyclerWithTop(recycler, topEdge + offset);
        } else {
            // 上滑
            removeAndRecyclerWithBottom(recycler, bottomEdge + offset);
        }

    }
    if (offset >= 0) {
        fillBottom(recycler, bottomEdge + offset);
    } else {
        fillTop(recycler, topEdge + offset);
    }
}
 
Example #14
Source File: StickyFooterBehaviorTest.java    From brickkit-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testTranslateStickyView() {
    when(adapter.getRecyclerView().getChildCount()).thenReturn(ADAPTER_COUNT);
    when(adapter.getItemCount()).thenReturn(ADAPTER_COUNT);
    when(adapter.getRecyclerView().getChildAt(8)).thenReturn(null);
    footerBehavior.translateStickyView();
    verify(recyclerView).getChildAt(8);
    assertNull(recyclerView.getChildAt(8));

    View textView = new TextView(context);
    when(adapter.getRecyclerView().getChildAt(8)).thenReturn(textView);
    when(adapter.getRecyclerView().getChildAdapterPosition(textView)).thenReturn(RecyclerView.NO_POSITION);
    footerBehavior.translateStickyView();
    verify(footerBehavior, atLeastOnce()).getStickyViewPosition(RecyclerView.NO_POSITION);

    when(adapter.getRecyclerView().getChildAdapterPosition(textView)).thenReturn(FOOTER_INDEX);
    BaseBrick footer = mock(BaseBrick.class);
    when(adapter.getSectionFooter(FOOTER_INDEX)).thenReturn(footer);
    when(adapter.indexOf(footer)).thenReturn(FOOTER_INDEX);
    textView.layout(-BOUNDARY_AXIS, -BOUNDARY_AXIS, -BOUNDARY_AXIS, -BOUNDARY_AXIS);
    footerBehavior.translateStickyView();
    verify(footerBehavior, atLeastOnce()).getStickyViewPosition(FOOTER_INDEX);

    LinearLayoutManager layoutManager = new LinearLayoutManager(context);
    layoutManager.setOrientation(OrientationHelper.VERTICAL);
    when(recyclerView.getLayoutManager()).thenReturn(layoutManager);
    footerBehavior.translateStickyView();
    assertEquals(((LinearLayoutManager)recyclerView.getLayoutManager()).getOrientation(), OrientationHelper.VERTICAL);

    when(adapter.getRecyclerView()). thenReturn(null);
    footerBehavior.translateStickyView();
    assertNull(adapter.getRecyclerView());

    when(dataManager.getBrickRecyclerAdapter()).thenReturn(null);
    footerBehavior.translateStickyView();
    assertNull(dataManager.getBrickRecyclerAdapter());
}
 
Example #15
Source File: BrickFragmentTest.java    From brickkit-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnCreateView() {
    testBrickFragment.setOrientation(OrientationHelper.HORIZONTAL);
    View view = testBrickFragment.onCreateView(inflater, null, null);
    assertNotNull(view);

    testBrickFragment.setOrientation(OrientationHelper.VERTICAL);
    view = testBrickFragment.onCreateView(inflater, null, null);
    assertNotNull(view);
}
 
Example #16
Source File: Demos.java    From litho-picasso with MIT License 5 votes vote down vote up
public static void initialize(Context context) {
  final ComponentContext c = new ComponentContext(context);
  final RecyclerBinder glideRecyclerBinder = new RecyclerBinder.Builder().layoutInfo(
      new LinearLayoutInfo(c, OrientationHelper.VERTICAL, false)).build(c);
  DataModel.populateBinderWithSampleDataForPicasso(glideRecyclerBinder, c);

  demoModels = new LinkedHashMap<>();
  demoModels.put(
      "Lithography - Picasso",
      LithographyRootComponent.create(c)
          .recyclerBinder(glideRecyclerBinder)
          .build());
  demoModels.put("Playground", PlaygroundComponent.create(c).build());
}
 
Example #17
Source File: RecyclerViewPositionHelper.java    From UltimateRecyclerView with Apache License 2.0 5 votes vote down vote up
private View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,
                                 boolean acceptPartiallyVisible) {
    OrientationHelper helper;
    if (layoutManager.canScrollVertically()) {
        helper = OrientationHelper.createVerticalHelper(layoutManager);
    } else {
        helper = OrientationHelper.createHorizontalHelper(layoutManager);
    }

    final int start = helper.getStartAfterPadding();
    final int end = helper.getEndAfterPadding();
    final int next = toIndex > fromIndex ? 1 : -1;
    View partiallyVisible = null;
    for (int i = fromIndex; i != toIndex; i += next) {
        final View child = layoutManager.getChildAt(i);
        final int childStart = helper.getDecoratedStart(child);
        final int childEnd = helper.getDecoratedEnd(child);
        if (childStart < end && childEnd > start) {
            if (completelyVisible) {
                if (childStart >= start && childEnd <= end) {
                    return child;
                } else if (acceptPartiallyVisible && partiallyVisible == null) {
                    partiallyVisible = child;
                }
            } else {
                return child;
            }
        }
    }
    return partiallyVisible;
}
 
Example #18
Source File: GravityDelegate.java    From GetApk with MIT License 5 votes vote down vote up
private int distanceToStart(View targetView, OrientationHelper helper, boolean fromEnd) {
    if (isRtlHorizontal && !fromEnd) {
        return distanceToEnd(targetView, helper, true);
    }

    return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
}
 
Example #19
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 5 votes vote down vote up
/**
 * @param child  计算的view
 * @param offset view的滑动偏移量
 * @return 返回view距离中心轴的距离
 */
private int calculateDistanceToCenter(View child, float offset) {
    final OrientationHelper orientationHelper = getOrientationHelper();
    final int centerToStart = (orientationHelper.getEndAfterPadding() - orientationHelper.getStartAfterPadding()) / 2 + orientationHelper.getStartAfterPadding();
    if (mOrientation == LinearLayout.HORIZONTAL) {
        return (int) (child.getWidth() / 2 - offset + child.getLeft() - centerToStart);
    } else {
        return (int) (child.getHeight() / 2 - offset + child.getTop() - centerToStart);
    }
}
 
Example #20
Source File: LearningRecyclerBinderComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(ComponentContext c) {
  final RecyclerBinder recyclerBinder =
      new RecyclerBinder.Builder()
          .layoutInfo(new LinearLayoutInfo(c, OrientationHelper.VERTICAL, false))
          .build(c);

  for (int i = 0; i < 32; i++) {
    recyclerBinder.insertItemAt(
        i, LearningPropsComponent.create(c).text1("Item: " + i).text2("Item: " + i).build());
  }

  return Recycler.create(c).binder(recyclerBinder).build();
}
 
Example #21
Source File: GridRecyclerConfiguration.java    From litho with Apache License 2.0 5 votes vote down vote up
private static void validate(GridRecyclerConfiguration configuration) {
  int snapMode = configuration.getSnapMode();
  if (configuration.getOrientation() == OrientationHelper.VERTICAL
      && !(snapMode == SNAP_NONE || snapMode == SnapUtil.SNAP_TO_START)) {
    throw new UnsupportedOperationException(
        "Only snap to start is implemented for vertical lists");
  }
}
 
Example #22
Source File: ListRecyclerConfiguration.java    From litho with Apache License 2.0 5 votes vote down vote up
private static void validate(ListRecyclerConfiguration configuration) {
  int snapMode = configuration.getSnapMode();
  if (configuration.getOrientation() == OrientationHelper.VERTICAL
      && !(snapMode == SNAP_NONE || snapMode == SNAP_TO_START)) {
    throw new UnsupportedOperationException(
        "Only snap to start is implemented for vertical lists");
  }
}
 
Example #23
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 5 votes vote down vote up
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
    if (mOrientation == LinearLayout.VERTICAL) {
        return 0;
    }
    if (getChildCount() == 0 || dx == 0) {
        return 0;
    }
    int offset = -dx;
    final OrientationHelper orientationHelper = getOrientationHelper();
    final int centerToStart = (orientationHelper.getEndAfterPadding() - orientationHelper.getStartAfterPadding()) / 2 + orientationHelper.getStartAfterPadding();
    View child;
    if (dx > 0) {
        child = getChildAt(getChildCount() - 1);
        if (child != null && getPosition(child) == getItemCount() - 1 && !isLooper) {
            // 计算全部加载完后item的偏移量,右边会留出空隙
            offset = -Math.max(0, Math.min(dx, (child.getRight() - child.getLeft()) / 2 + child.getLeft() - centerToStart));
        }
    } else {
        child = getChildAt(0);
        if (mFirstVisiblePosition == 0 && child != null && !isLooper) {
            // 计算首次加载item的偏移量,左边会留出空隙
            offset = -Math.min(0, Math.max(dx, ((child.getRight() - child.getLeft()) / 2 + child.getLeft()) - centerToStart));
        }
    }
    // 记录偏移量
    getState().scrollOffset = -offset;
    fill(recycler, -offset);
    offsetChildrenHorizontal(offset);
    return -offset;
}
 
Example #24
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Ignore("t33888191") // TODO(t33888191): Support wrapContent with insertAsync
@Test
public void testWrapContentWithUpdateRangeAsyncOnHorizontal() {
  final int NUM_TO_UPDATE = 5;
  final int widthSpec = makeSizeSpec(1000, AT_MOST);
  final int heightSpec = makeSizeSpec(1000, EXACTLY);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.HORIZONTAL, 100, true);

  recyclerBinder.mount(mRecyclerView);

  final ArrayList<RenderInfo> newRenderInfos = new ArrayList<>();
  for (int i = 0; i < NUM_TO_UPDATE; i++) {
    final Component component =
        TestDrawableComponent.create(mComponentContext).measuredWidth(50).build();
    newRenderInfos.add(ComponentRenderInfo.create().component(component).build());
  }
  recyclerBinder.updateRangeAt(0, newRenderInfos);
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  mLayoutThreadShadowLooper.runToEndOfTasks();

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.width).isEqualTo(550);
}
 
Example #25
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Ignore("t33888191") // TODO(t33888191): Support wrapContent with insertAsync
@Test
public void testWrapContentWithUpdateAsyncOnHorizontal() {
  final int widthSpec = makeSizeSpec(1000, AT_MOST);
  final int heightSpec = makeSizeSpec(1000, EXACTLY);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.HORIZONTAL, 100, true);

  recyclerBinder.mount(mRecyclerView);

  final Component newComponent =
      new InlineLayoutSpec() {
        @Override
        protected Component onCreateLayout(ComponentContext c) {
          return TestDrawableComponent.create(c).measuredWidth(200).build();
        }
      };
  recyclerBinder.updateItemAt(0, newComponent);
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  mLayoutThreadShadowLooper.runToEndOfTasks();

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.width).isEqualTo(900);
}
 
Example #26
Source File: RecyclerViewPositionHelper.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,
                         boolean acceptPartiallyVisible) {
    OrientationHelper helper;
    if (linearLayoutManager.canScrollVertically()) {
        helper = OrientationHelper.createVerticalHelper(linearLayoutManager);
    } else {
        helper = OrientationHelper.createHorizontalHelper(linearLayoutManager);
    }

    final int start = helper.getStartAfterPadding();
    final int end = helper.getEndAfterPadding();
    final int next = toIndex > fromIndex ? 1 : -1;
    View partiallyVisible = null;
    for (int i = fromIndex; i != toIndex; i += next) {
        final View child = linearLayoutManager.getChildAt(i);
        final int childStart = helper.getDecoratedStart(child);
        final int childEnd = helper.getDecoratedEnd(child);
        if (childStart < end && childEnd > start) {
            if (completelyVisible) {
                if (childStart >= start && childEnd <= end) {
                    return child;
                } else if (acceptPartiallyVisible && partiallyVisible == null) {
                    partiallyVisible = child;
                }
            } else {
                return child;
            }
        }
    }
    return partiallyVisible;
}
 
Example #27
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateWithWrapContent() {
  final int widthSpec = makeSizeSpec(10, EXACTLY);
  final int heightSpec = makeSizeSpec(10, EXACTLY);
  Size size = new Size();
  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 2, OrientationHelper.VERTICAL, 1000, false);
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));

  recyclerBinder.mount(mRecyclerView);
  final boolean[] wasCreated = new boolean[1];
  wasCreated[0] = false;

  final Component component =
      new InlineLayoutSpec() {
        @Override
        protected Component onCreateLayout(ComponentContext c) {
          TestDrawableComponent.Builder builder = TestDrawableComponent.create(c);
          wasCreated[0] = true;
          return builder.build();
        }
      };
  recyclerBinder.updateItemAt(1, ComponentRenderInfo.create().component(component).build());
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  mLayoutThreadShadowLooper.runToEndOfTasks();

  assertThat(wasCreated[0]).isTrue();
}
 
Example #28
Source File: RecyclerViewPositionHelper.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,
                         boolean acceptPartiallyVisible) {
  OrientationHelper helper;
  if (layoutManager.canScrollVertically()) {
    helper = OrientationHelper.createVerticalHelper(layoutManager);
  } else {
    helper = OrientationHelper.createHorizontalHelper(layoutManager);
  }

  final int start = helper.getStartAfterPadding();
  final int end = helper.getEndAfterPadding();
  final int next = toIndex > fromIndex ? 1 : -1;
  View partiallyVisible = null;
  for (int i = fromIndex; i != toIndex; i += next) {
    final View child = layoutManager.getChildAt(i);
    final int childStart = helper.getDecoratedStart(child);
    final int childEnd = helper.getDecoratedEnd(child);
    if (childStart < end && childEnd > start) {
      if (completelyVisible) {
        if (childStart >= start && childEnd <= end) {
          return child;
        } else if (acceptPartiallyVisible && partiallyVisible == null) {
          partiallyVisible = child;
        }
      } else {
        return child;
      }
    }
  }
  return partiallyVisible;
}
 
Example #29
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrapContentWithUpdateOnHorizontal() {
  final int widthSpec = makeSizeSpec(1000, AT_MOST);
  final int heightSpec = makeSizeSpec(1000, EXACTLY);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.HORIZONTAL, 100);

  recyclerBinder.mount(mRecyclerView);

  final Component newComponent =
      new InlineLayoutSpec() {
        @Override
        protected Component onCreateLayout(ComponentContext c) {
          return TestDrawableComponent.create(c).measuredWidth(200).build();
        }
      };
  recyclerBinder.updateItemAt(0, newComponent);
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.width).isEqualTo(900);
}
 
Example #30
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 5 votes vote down vote up
private OrientationHelper getOrientationHelper() {
    if (mOrientation == LinearLayout.HORIZONTAL) {
        if (mHorizontalHelper == null) {
            mHorizontalHelper = OrientationHelper.createHorizontalHelper(this);
        }
        return mHorizontalHelper;
    } else {
        if (mVerticalHelper == null) {
            mVerticalHelper = OrientationHelper.createVerticalHelper(this);
        }
        return mVerticalHelper;
    }
}