androidx.recyclerview.widget.RecyclerView.LayoutManager Java Examples

The following examples show how to use androidx.recyclerview.widget.RecyclerView.LayoutManager. 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: MainActivity.java    From connectivity-samples with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mOutputTextView = findViewById(R.id.access_point_summary_text_view);
    mRecyclerView = findViewById(R.id.recycler_view);

    // Improve performance if you know that changes in content do not change the layout size
    // of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    LayoutManager layoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(layoutManager);

    mAccessPointsSupporting80211mc = new ArrayList<>();

    mAdapter = new MyAdapter(mAccessPointsSupporting80211mc, this);
    mRecyclerView.setAdapter(mAdapter);

    mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    mWifiScanReceiver = new WifiScanReceiver();
}
 
Example #2
Source File: DividerGridItemDecorationUtils.java    From shinny-futures-android with GNU General Public License v3.0 6 votes vote down vote up
private boolean isLastColumn(RecyclerView parent, int pos, int spanCount,
                             int childCount) {
    LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
        {
            return true;
        }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        int orientation = ((StaggeredGridLayoutManager) layoutManager)
                .getOrientation();
        if (orientation == StaggeredGridLayoutManager.VERTICAL) {
            if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
            {
                return true;
            }
        } else {
            childCount = childCount - childCount % spanCount;
            if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
                return true;
        }
    }
    return false;
}
 
Example #3
Source File: ColumnProperties.java    From DragListView with Apache License 2.0 6 votes vote down vote up
private ColumnProperties(DragItemAdapter adapter,
                         LayoutManager layoutManager,
                         List<RecyclerView.ItemDecoration> itemDecorations,
                         boolean hasFixedItemSize,
                         int columnBackgroundColor,
                         int itemsSectionBackgroundColor,
                         View columnDragView,
                         View header) {
    mDragItemAdapter = adapter;
    mLayoutManager = layoutManager;
    mItemDecorations = itemDecorations;
    mHasFixedItemSize = hasFixedItemSize;
    mColumnBackgroundColor = columnBackgroundColor;
    mItemsSectionBackgroundColor = itemsSectionBackgroundColor;
    mHeader = header;
    mColumnDragView = columnDragView;
}
 
Example #4
Source File: MainActivity.java    From android-WifiRttScan with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mOutputTextView = findViewById(R.id.access_point_summary_text_view);
    mRecyclerView = findViewById(R.id.recycler_view);

    // Improve performance if you know that changes in content do not change the layout size
    // of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    LayoutManager layoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(layoutManager);

    mAccessPointsSupporting80211mc = new ArrayList<>();

    mAdapter = new MyAdapter(mAccessPointsSupporting80211mc, this);
    mRecyclerView.setAdapter(mAdapter);

    mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    mWifiScanReceiver = new WifiScanReceiver();
}
 
Example #5
Source File: StartSnapHelper.java    From litho with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public int[] calculateDistanceToFinalSnap(
    @NonNull LayoutManager layoutManager, @NonNull View targetView) {
  int[] out = new int[2];
  if (layoutManager.canScrollHorizontally()) {
    out[0] = distanceToStart(layoutManager, targetView, getHorizontalHelper(layoutManager));
  } else {
    out[0] = 0;
  }

  if (layoutManager.canScrollVertically()) {
    out[1] = distanceToStart(layoutManager, targetView, getVerticalHelper(layoutManager));
  } else {
    out[1] = 0;
  }
  return out;
}
 
Example #6
Source File: StartSnapHelper.java    From litho with Apache License 2.0 6 votes vote down vote up
/** @return the first View whose start is before the start of this recycler view */
@Nullable
private static View findFirstViewBeforeStart(
    LayoutManager layoutManager, OrientationHelper helper) {
  int childCount = layoutManager.getChildCount();
  if (childCount == 0) {
    return null;
  }

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

  for (int i = 0; i < childCount; i++) {
    final View child = layoutManager.getChildAt(i);
    int childStart = helper.getDecoratedStart(child);
    int absDistance = Math.abs(childStart - start);

    if (childStart < start && absDistance < absClosest) {
      absClosest = absDistance;
      closestChild = child;
    }
  }

  return closestChild;
}
 
Example #7
Source File: DividerGridItemDecorationUtils.java    From shinny-futures-android with GNU General Public License v3.0 5 votes vote down vote up
private int getSpanCount(RecyclerView parent) {
    // 列数
    int spanCount = -1;
    LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {

        spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        spanCount = ((StaggeredGridLayoutManager) layoutManager)
                .getSpanCount();
    }
    return spanCount;
}
 
Example #8
Source File: ArtistViewModel.java    From Jockey with Apache License 2.0 5 votes vote down vote up
@Bindable
public LayoutManager getLayoutManager() {
    mAlbumColumnCount = ViewUtils.getNumberOfGridColumns(getContext(), R.dimen.grid_width);
    mRelatedColumnCount = ViewUtils.getNumberOfGridColumns(getContext(), R.dimen.large_grid_width);
    mColumnCount = mAlbumColumnCount * mRelatedColumnCount;

    // Setup the GridLayoutManager
    GridLayoutManager layoutManager = new GridLayoutManager(getContext(), mColumnCount);
    GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            // Albums & related artists fill one column,
            // all other view types fill the available width
            boolean isArtist = mAlbumSection != null
                    && mAdapter.getItemViewType(position) == mAlbumSection.getTypeId();
            boolean isRelatedArtist = mRelatedArtistSection != null
                    && mAdapter.getItemViewType(position) == mRelatedArtistSection.getTypeId();

            if (isArtist) {
                return mRelatedColumnCount;
            } else if (isRelatedArtist) {
                return mAlbumColumnCount;
            } else {
                return mColumnCount;
            }
        }
    };

    spanSizeLookup.setSpanIndexCacheEnabled(true);
    layoutManager.setSpanSizeLookup(spanSizeLookup);

    return layoutManager;
}
 
Example #9
Source File: EpoxyItemSpacingDecorator.java    From epoxy with Apache License 2.0 5 votes vote down vote up
private static boolean shouldReverseLayout(LayoutManager layout, boolean horizontallyScrolling) {
  boolean reverseLayout =
      layout instanceof LinearLayoutManager && ((LinearLayoutManager) layout).getReverseLayout();
  boolean rtl = layout.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL;
  if (horizontallyScrolling && rtl) {
    // This is how linearlayout checks if it should reverse layout in #resolveShouldLayoutReverse
    reverseLayout = !reverseLayout;
  }

  return reverseLayout;
}
 
Example #10
Source File: RecyclerBinder.java    From litho with Apache License 2.0 5 votes vote down vote up
private boolean getStackFromEnd() {
  final LayoutManager layoutManager = mLayoutInfo.getLayoutManager();
  if (layoutManager instanceof LinearLayoutManager) {
    return ((LinearLayoutManager) layoutManager).getStackFromEnd();
  } else {
    return false;
  }
}
 
Example #11
Source File: RecyclerBinder.java    From litho with Apache License 2.0 5 votes vote down vote up
private boolean getReverseLayout() {
  final LayoutManager layoutManager = mLayoutInfo.getLayoutManager();
  if (layoutManager instanceof LinearLayoutManager) {
    return ((LinearLayoutManager) layoutManager).getReverseLayout();
  } else {
    return false;
  }
}
 
Example #12
Source File: StartSnapHelper.java    From litho with Apache License 2.0 5 votes vote down vote up
@NonNull
private OrientationHelper getHorizontalHelper(@NonNull LayoutManager layoutManager) {
  if (mHorizontalHelper == null || mHorizontalHelperLayoutManager != layoutManager) {
    mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
    mHorizontalHelperLayoutManager = layoutManager;
  }

  return mHorizontalHelper;
}
 
Example #13
Source File: StartSnapHelper.java    From litho with Apache License 2.0 5 votes vote down vote up
@NonNull
private OrientationHelper getVerticalHelper(@NonNull LayoutManager layoutManager) {
  if (mVerticalHelper == null || mVerticalHelperLayoutManager != layoutManager) {
    mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager);
    mVerticalHelperLayoutManager = layoutManager;
  }

  return mVerticalHelper;
}
 
Example #14
Source File: StartSnapHelper.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * Return the child view that is currently closest to the start of this parent.
 *
 * @param layoutManager The {@link LayoutManager} associated with the attached {@link
 *     RecyclerView}.
 * @param helper The relevant {@link OrientationHelper} for the attached {@link RecyclerView}.
 * @return the child view that is currently closest to the start of this parent.
 */
@Nullable
private static View findViewClosestToStart(
    LayoutManager layoutManager, OrientationHelper helper) {
  int childCount = layoutManager.getChildCount();
  if (childCount == 0) {
    return null;
  }

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

  for (int i = 0; i < childCount; i++) {
    final View child = layoutManager.getChildAt(i);
    int childStart = helper.getDecoratedStart(child);
    int absDistance = Math.abs(childStart - start);

    /** if child start is closer than previous closest, set it as closest * */
    if (absDistance < absClosest) {
      absClosest = absDistance;
      closestChild = child;
    }
  }

  return closestChild;
}
 
Example #15
Source File: StartSnapHelper.java    From litho with Apache License 2.0 5 votes vote down vote up
private int distanceToStart(
    @NonNull RecyclerView.LayoutManager layoutManager,
    @NonNull View targetView,
    OrientationHelper helper) {
  final int childStart = helper.getDecoratedStart(targetView);
  final int containerStart = helper.getStartAfterPadding();
  return childStart - containerStart;
}
 
Example #16
Source File: StartSnapHelper.java    From litho with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public View findSnapView(LayoutManager layoutManager) {
  if (layoutManager.canScrollVertically()) {
    return findViewClosestToStart(layoutManager, getVerticalHelper(layoutManager));
  } else if (layoutManager.canScrollHorizontally()) {
    return findViewClosestToStart(layoutManager, getHorizontalHelper(layoutManager));
  }
  return null;
}
 
Example #17
Source File: DividerGridItemDecorationUtils.java    From shinny-futures-android with GNU General Public License v3.0 5 votes vote down vote up
private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
                          int childCount) {
    LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        childCount = childCount - childCount % spanCount;
        if (pos >= childCount)// 如果是最后一行,则不需要绘制底部
            return true;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        int orientation = ((StaggeredGridLayoutManager) layoutManager)
                .getOrientation();
        // StaggeredGridLayoutManager 且纵向滚动
        if (orientation == StaggeredGridLayoutManager.VERTICAL) {
            childCount = childCount - childCount % spanCount;
            // 如果是最后一行,则不需要绘制底部
            if (pos >= childCount)
                return true;
        } else
        // StaggeredGridLayoutManager 且横向滚动
        {
            // 如果是最后一行,则不需要绘制底部
            if ((pos + 1) % spanCount == 0) {
                return true;
            }
        }
    }
    return false;
}
 
Example #18
Source File: RecyclerBinder.java    From litho with Apache License 2.0 4 votes vote down vote up
/**
 * Call from the owning {@link Component}'s onUnmount. This is where the adapter is removed from
 * the {@link RecyclerView}.
 *
 * @param view the {@link RecyclerView} being unmounted.
 */
@UiThread
@Override
public void unmount(RecyclerView view) {
  ThreadUtils.assertMainThread();
  if (mIsSubAdapter) {
    throw new RuntimeException("Can't unmount a RecyclerView in sub adapter mode");
  }

  final LayoutManager layoutManager = mLayoutInfo.getLayoutManager();
  final View firstView = layoutManager.findViewByPosition(mCurrentFirstVisiblePosition);

  if (firstView != null) {
    final boolean reverseLayout = getReverseLayout();

    if (mLayoutInfo.getScrollDirection() == HORIZONTAL) {
      mCurrentOffset =
          reverseLayout
              ? view.getWidth()
                  - layoutManager.getPaddingRight()
                  - layoutManager.getDecoratedRight(firstView)
              : layoutManager.getDecoratedLeft(firstView) - layoutManager.getPaddingLeft();
    } else {
      mCurrentOffset =
          reverseLayout
              ? view.getHeight()
                  - layoutManager.getPaddingBottom()
                  - layoutManager.getDecoratedBottom(firstView)
              : layoutManager.getDecoratedTop(firstView) - layoutManager.getPaddingTop();
    }
  } else {
    mCurrentOffset = 0;
  }

  view.removeOnScrollListener(mViewportManager.getScrollListener());

  unregisterDrawListener(view);
  maybeDispatchDataRendered();

  view.setAdapter(null);
  view.setLayoutManager(null);

  mViewportManager.removeViewportChangedListener(mViewportChangedListener);

  // We might have already unmounted this view when calling mount with a different view. In this
  // case we can just return here.
  if (mMountedView != view) {
    return;
  }

  mMountedView = null;
  if (mStickyHeaderController != null) {
    mStickyHeaderController.reset();
  }

  mLayoutInfo.setRenderInfoCollection(null);
}
 
Example #19
Source File: LayoutInfo.java    From litho with Apache License 2.0 4 votes vote down vote up
/** @return The {@link LayoutManager} to be used with the {@link RecyclerView}. */
LayoutManager getLayoutManager();
 
Example #20
Source File: StartSnapHelper.java    From litho with Apache License 2.0 4 votes vote down vote up
@Override
public int findTargetSnapPosition(LayoutManager layoutManager, int velocityX, int velocityY) {
  final int itemCount = layoutManager.getItemCount();
  if (itemCount == 0) {
    return RecyclerView.NO_POSITION;
  }

  final boolean isHorizontal = layoutManager.canScrollHorizontally();
  final OrientationHelper orientationHelper =
      isHorizontal ? getHorizontalHelper(layoutManager) : getVerticalHelper(layoutManager);
  final View firstBeforeStartChild = findFirstViewBeforeStart(layoutManager, orientationHelper);

  if (firstBeforeStartChild == null) {
    return RecyclerView.NO_POSITION;
  }
  final int firstBeforeStartPosition = layoutManager.getPosition(firstBeforeStartChild);
  if (firstBeforeStartPosition == RecyclerView.NO_POSITION) {
    return RecyclerView.NO_POSITION;
  }

  final boolean forwardDirection = isHorizontal ? velocityX > 0 : velocityY > 0;

  boolean reverseLayout = false;
  if ((layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
    RecyclerView.SmoothScroller.ScrollVectorProvider vectorProvider =
        (RecyclerView.SmoothScroller.ScrollVectorProvider) layoutManager;
    PointF vectorForEnd = vectorProvider.computeScrollVectorForPosition(itemCount - 1);
    if (vectorForEnd != null) {
      reverseLayout = vectorForEnd.x < 0 || vectorForEnd.y < 0;
    }
  }

  int targetPos =
      forwardDirection
          ? (reverseLayout
              ? firstBeforeStartPosition - mFlingOffset
              : firstBeforeStartPosition + mFlingOffset)
          : firstBeforeStartPosition;
  if (targetPos < 0) {
    targetPos = 0;
  }
  if (targetPos >= itemCount) {
    targetPos = itemCount - 1;
  }
  return targetPos;
}
 
Example #21
Source File: ColumnProperties.java    From DragListView with Apache License 2.0 4 votes vote down vote up
LayoutManager getLayoutManager() {
    return mLayoutManager;
}
 
Example #22
Source File: ColumnProperties.java    From DragListView with Apache License 2.0 2 votes vote down vote up
/**
 * Sets {@link LayoutManager} for items' list {@link RecyclerView}. By default is used {@link LinearLayoutManager}
 *
 * @param layoutManager A layout manager for items' list {@link RecyclerView}. By default is used {@link LinearLayoutManager}.
 *                      set null to use default value.
 *
 * @return instance of the {@link Builder}
 */
public Builder setLayoutManager(LayoutManager layoutManager) {
    mLayoutManager = layoutManager;
    return this;
}