Giter Site home page Giter Site logo

shenbengit / pagergridlayoutmanager Goto Github PK

View Code? Open in Web Editor NEW
228.0 5.0 25.0 13.36 MB

基于RecyclerView实现网格分页LayoutManager——PagerGridLayoutManager

License: MIT License

Java 100.00%
android recyclerview recyclerview-layoutmanager page grid

pagergridlayoutmanager's Introduction

PagerGridLayoutManager

基于RecyclerView实现网格分页布局——PagerGridLayoutManager

运行效果

滑动方向 设置行数列数 滚动到指定位置
滑动方向 设置行数列数 滚动到指定位置
滚动到指定页 其他操作
滚动到指定页 其他操作
ViewPager中使用 ViewPager2中使用
ViewPager中使用 ViewPager2中使用

功能特点

  • 水平垂直分页滑动
  • 复用机制和视图回收
  • 支持scrollToPosition()和smoothScrollToPosition()
  • 兼容输入法弹出导致onLayoutChildren()方法重新调用的问题
  • 支持scrollBar
  • 状态恢复
  • 滑动冲突处理
  • 支持clipToPadding=false
  • 支持Reverse Layout,兼容RTL

引入

将JitPack存储库添加到您的项目中(项目根目录下build.gradle文件)

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

添加依赖

change log

从v1.1.0版本开始,水平滑动排列方式改为先从左到右,再从上到下;若您需要水平滑动排列方式改为先从上到下,再从左到右的方式,请查看分支1.0.x
在您引入项目的build.gradle中添加:

dependencies {
    //水平排列方式:先从左到右,再从上到下。最新版本。
    //Horizontal arrangement: from left to right, then from top to bottom. Latest version.
    implementation 'com.github.shenbengit:PagerGridLayoutManager:Tag'
    //或者 or 
    //水平排列方式:先从上到下,再从左到右。最终版本。
    //Horizontal arrangement: from top to bottom, then from left to right. Final version.
    //弃用,Deprecated.
    implementation 'com.github.shenbengit:PagerGridLayoutManager:1.0.6'
}

快速使用

注意事项

1、RecyclerView的宽高必须指定,match_parent或者例如100dp等。 (RecyclerView's width and height must be exactly. )
2、Item布局的宽高必须是match_parent。(item layout's width and height must use match_parent.)
3、在ViewPager中使用是正常的,ViewPager已经处理好了滑动冲突。
4、在ViewPager2中使用存在滑动冲突,ViewPager2未做滑动冲突处理,本库已经处理滑动冲突,若不满足您的需求可自行处理。

使用PagerGridLayoutManager

//是否开启调试日志
PagerGridLayoutManager.setDebug(BuildConfig.DEBUG);

PagerGridLayoutManager layoutManager = new PagerGridLayoutManager(
        /*rows*/3, 
        /*columns*/ 3, 
        /*PagerGridLayoutManager.VERTICAL*/PagerGridLayoutManager.HORIZONTAL, 
        /*reverseLayout*/ false
);
/*
是否启用处理滑动冲突滑动冲突,default: true;若不需要库中自带的处理方式,则置为false,自行处理。
setHandlingSlidingConflictsEnabled() 必须要在{@link RecyclerView#setLayoutManager(RecyclerView.LayoutManager)} 之前调用,否则无效
you must call this method before {@link RecyclerView#setLayoutManager(RecyclerView.LayoutManager)}
*/
layoutManager.setHandlingSlidingConflictsEnabled(true);

recyclerView.setLayoutManager(layoutManager);

//设置监听
layoutManager.setPagerChangedListener(new PagerGridLayoutManager.PagerChangedListener() {
    /**
     * 页数回调
     * 仅会在页数变化时回调
     * @param pagerCount 页数,从1开始,为0时说明无数据
     */
    @Override
    public void onPagerCountChanged(int pagerCount) {
        Log.w(TAG, "onPagerCountChanged-pagerCount:" + pagerCount);
    }

    /**
     * 选中的页面下标
     * 从0开始
     * @param prePagerIndex     上次的页码,当{{@link PagerGridLayoutManager#getItemCount()}}为0时,为-1,{{@link PagerGridLayoutManager#NO_ITEM}}
     * @param currentPagerIndex 当前的页码,当{{@link PagerGridLayoutManager#getItemCount()}}为0时,为-1,{{@link PagerGridLayoutManager#NO_ITEM}}
     */
    @Override
    public void onPagerIndexSelected(int prePagerIndex, int currentPagerIndex) {
        Log.w(TAG, "onPagerIndexSelected-prePagerIndex " + prePagerIndex + ",currentPagerIndex:" + currentPagerIndex);
    }
});
//设置滑动方向
layoutManager.setOrientation(/*PagerGridLayoutManager.HORIZONTAL*/ PagerGridLayoutManager.VERTICAL);
/*
是否反向布局,自动兼容RTL;
注意:水平方向反向是排列顺序和滑动放向都反向,垂直方向仅排列顺序反向;
Whether the layout is reversed, automatically compatible with RTL;
Note: The horizontal reverse is the reverse of the arrangement order and the sliding direction, and the vertical direction is only the reverse of the arrangement order;
*/
layoutManager.setReverseLayout(/*true*/ false);
//设置行数
layoutManager.setRows(2);
//设置列数
layoutManager.setColumns(2);

//滚动到指定位置,注意:这个方法只会滚动到目标位置所在的页。
recyclerView.scrollToPosition(10);
//平滑滚动到指定位置,注意:这个方法只会滚动到目标位置所在的页。
recyclerView.smoothScrollToPosition(10);

//滚动到指定页
layoutManager.scrollToPagerIndex(3);
//平滑滚动到指定页,注意:如果滚动的页与当前页超过3,避免长时间滚动,会先直接滚动到就近的页,再做平滑滚动
layoutManager.smoothScrollToPagerIndex(6);
//滚动到上一页
layoutManager.scrollToPrePager();
//滚动到下一页
layoutManager.scrollToNextPager();
//平滑滚动到上一页
layoutManager.smoothScrollToPrePager();
//平滑滚动到下一页
layoutManager.smoothScrollToNextPager();
//设置滑动每像素需要花费的时间,不可过小,不然可能会出现划过再回退的情况。默认值:70
layoutManager.setMillisecondPreInch(70);
//设置最大滚动时间,如果您想此值无效,请使用{@link Integer#MAX_VALUE}。默认值:200 ms
layoutManager.setMaxScrollOnFlingDuration(200);

proguard-rules.pro

此库不需要额外混淆

如果此库对您有用,欢迎给个star

作者其他的开源项目

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.