Running effect

Sliding direction Sets the number of rows Scroll to the specified position
Scroll to the specified page Other operating
Used in the ViewPager ViewPager2 used in

Functional features

  • Reuse mechanisms and view recycling
  • Support for scrollToPosition() and smoothScrollToPosition()
  • Compatible input methods pop up a problem that causes the onLayoutChildren() method to be called again
  • Support the scrollBar
  • back
  • Sliding conflict handling

The introduction of

Add the JitPack repository to your project (build.gradle file in the project root directory)

allprojects {
    repositories{... maven { url'https://jitpack.io'}}}Copy the code

Add the dependent

Add to build.gradle where you import the project

dependencies {
    implementation 'com.github.shenbengit:PagerGridLayoutManager:Tag'
}
Copy the code

Quick to use

Matters needing attention

1. The width and height of RecyclerView must be specified, match_parent or 100DP for example. 2, The width and height of item layout must be match_parent. (Item layout’s width and height must use match_parent.) 3, Use in ViewPager is normal, the ViewPager already handles sliding conflicts. 4, there is sliding conflict in ViewPager2, ViewPager2 did not do sliding conflict processing, this library has dealt with sliding conflict, if not to meet your needs can be handled by themselves.

Using PagerGridLayoutManager

// Whether to enable debug log
PagerGridLayoutManager.setDebug(BuildConfig.DEBUG);

PagerGridLayoutManager layoutManager = new PagerGridLayoutManager(/*rows*/3./*columns*/ 3./*PagerGridLayoutManager.VERTICAL*/PagerGridLayoutManager.HORIZONTAL);
/* Whether to enable sliding conflicts. If the built-in processing mode in the library is not required, set this parameter to false. SetHandlingSlidingConflictsEnabled () must be in {@ link RecyclerView# setLayoutManager (RecyclerView. LayoutManager)} before the call, Otherwise no you must call this method before {@ link RecyclerView# setLayoutManager (RecyclerView. LayoutManager)} * /
layoutManager.setHandlingSlidingConflictsEnabled(true);

recyclerView.setLayoutManager(layoutManager);

// Set the listener
layoutManager.setPagerChangedListener(new PagerGridLayoutManager.PagerChangedListener() {
    /** * page callback * will only be called when the number of pages changes *@paramPagerCount Number of pages, starting from 1 */
    @Override
    public void onPagerCountChanged(int pagerCount) {
        Log.w(TAG, "onPagerCountChanged-pagerCount:" + pagerCount);
    }

    /** * The selected page subscripts start from 0 *@paramPrePagerIndex Last page number when {{@linkPagerGridLayoutManager#getItemCount()}} -1, {{@link PagerGridLayoutManager#NO_ITEM}}
     * @paramCurrentPagerIndex Specifies the current page number when {{@linkPagerGridLayoutManager#getItemCount()}} -1, {{@link PagerGridLayoutManager#NO_ITEM}}
     */
    @Override
    public void onPagerIndexSelected(int prePagerIndex, int currentPagerIndex) {
        Log.w(TAG, "onPagerIndexSelected-prePagerIndex " + prePagerIndex + ",currentPagerIndex:"+ currentPagerIndex); }});// Set the sliding direction, note: horizontal and vertical order is not the same.
layoutManager.setOrientation(/*PagerGridLayoutManager.HORIZONTAL*/PagerGridLayoutManager.VERTICAL);
// Set the number of lines
layoutManager.setRows(2);
// Set the number of columns
layoutManager.setColumns(2);

// Scroll to the specified position. Note that this method only scrolls to the page at the target position.
recyclerView.scrollToPosition(10);
// Smooth scrolling to the specified position. Note that this method only scrolls to the page at the target position.
recyclerView.smoothScrollToPosition(10);

// Scroll to the specified page
layoutManager.scrollToPagerIndex(3);
// Smooth scroll to the specified page. Note: If the number of scrolling pages is more than 3, avoid long scrolling
layoutManager.smoothScrollToPagerIndex(6);
// Scroll to the previous page
layoutManager.scrollToPrePager();
// Scroll to next page
layoutManager.scrollToNextPager();
// Scroll smoothly to the previous page
layoutManager.smoothScrollToPrePager();
// Scroll smoothly to the next page
layoutManager.smoothScrollToNextPager();

Copy the code

If wrong or other suggestions welcome correction!

For details, please moveGithub