1 Star 2 Fork 1

冬季穿短裤 / PagerGridLayoutManager

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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

作者其他的开源项目

License

MIT License Copyright (c) 2021 沈犇 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于RecyclerView实现网格分页布局——PagerGridLayoutManager 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/qq714081644/PagerGridLayoutManager.git
git@gitee.com:qq714081644/PagerGridLayoutManager.git
qq714081644
PagerGridLayoutManager
PagerGridLayoutManager
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891