最简单实现网格布局分页效果 (recyclerview + snaphelper)

运行截图

                     

基于 PagerSnapHelper 自定义 GridPagerSnapHelper.

支持横向与纵向滑动,行列数量可自定义传入

 private void initGridePager() {
        // 3行3列,横向
        int colum = 3;
        int row = 3;
        int orientation = GridLayoutManager.HORIZONTAL;
        int spanCount = (GridLayoutManager.VERTICAL == orientation) ? colum : row;

        // 设置布局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), spanCount, orientation, false);
        mRecyclerView.setLayoutManager(gridLayoutManager);

        // 设置设配器
        GridPagerAdapter gridPagerAdapter = new GridPagerAdapter(getApplicationContext(), orientation, colum, row);
        mRecyclerView.setAdapter(gridPagerAdapter);

        // 设置页面滑动帮助类 sanaphelp
        GridPagerSnapHelper pagerSnapHelper = new GridPagerSnapHelper(colum, row);
        pagerSnapHelper.attachToRecyclerView(mRecyclerView);
    }

demo地址:

https://github.com/PPQingZhao/GridPagerDemo

猜你喜欢

转载自blog.csdn.net/qq_33635618/article/details/94739741
今日推荐