天天看点

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

运行截图

         

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

            

最简单实现网格布局分页效果 (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