实现功能在网格布局下GridLayoutManager实现横向滚动多行多列分页滚动效果
1.重写SnapHelper类
import android.graphics.PointF;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.support.v7.widget.LinearSmoothScroller;import android.support.v7.widget.OrientationHelper;import android.support.v7.widget.RecyclerView;import android.support.v7.widget.RecyclerView.LayoutManager;import android.support.v7.widget.RecyclerView.State;import android.support.v7.widget.RecyclerView.SmoothScroller.ScrollVectorProvider;import android.support.v7.widget.SnapHelper;import android.util.DisplayMetrics;import android.view.View;/*** Created by Muyuki on 2019/11/8* 网格布局分页效果* 仅限布局为GridLayoutManager使用*/public class GridPagerSnapHelper extends SnapHelper {Nullableprivate OrientationHelper mVerticalHelper;Nullableprivate OrientationHelper mHorizontalHelper;private RecyclerView recyclerView;private int rowCount 1;private int columCount 1;/*** param row 行* param column 列*/public GridPagerSnapHelper(int row,int column) {this.rowCount row;this.columCount column;}Overridepublic void attachToRecyclerView(Nullable RecyclerView recyclerView) throws IllegalStateException {super.attachToRecyclerView(recyclerView);this.recyclerView recyclerView;}NullableOverridepublic int[] calculateDistanceToFinalSnap(NonNull LayoutManager layoutManager, NonNull View targetView) {int[] out new int[2];if (layoutManager.canScrollHorizontally()) {out[0] this.distanceToStart(layoutManager, targetView, this.getHorizontalHelper(layoutManager));} else {out[0] 0;}if (layoutManager.canScrollVertically()) {out[1] this.distanceToStart(layoutManager, targetView, this.getVerticalHelper(layoutManager));} else {out[1] 0;}return out;}NullableOverridepublic View findSnapView(LayoutManager layoutManager) {if (layoutManager.canScrollVertically()) {return this.findStartSnapView(layoutManager, this.getVerticalHelper(layoutManager));} else {return layoutManager.canScrollHorizontally() ? this.findStartSnapView(layoutManager, this.getHorizontalHelper(layoutManager)) : null;}}Overridepublic int findTargetSnapPosition(LayoutManager layoutManager, int velocityX, int velocityY) {int itemCount layoutManager.getItemCount();if (itemCount 0) {return -1;} else {View mStartMostChildView null;if (layoutManager.canScrollVertically()) {mStartMostChildView this.findStartView(layoutManager, this.getVerticalHelper(layoutManager));} else if (layoutManager.canScrollHorizontally()) {mStartMostChildView this.findStartView(layoutManager, this.getHorizontalHelper(layoutManager));}if (mStartMostChildView null) {return -1;} else {int centerPosition layoutManager.getPosition(mStartMostChildView);if (centerPosition -1) {return -1;} else {// 计算当前页面索引int pagerIndex centerPosition / (rowCount * columCount);// 是否滑向下一页boolean forwardDirection;if (layoutManager.canScrollHorizontally()) {forwardDirection velocityX > 0;} else {forwardDirection velocityY > 0;}// 条目是否是翻转模式boolean reverseLayout false;if (layoutManager instanceof ScrollVectorProvider) {ScrollVectorProvider vectorProvider (ScrollVectorProvider) layoutManager;PointF vectorForEnd vectorProvider.computeScrollVectorForPosition(itemCount - 1);if (vectorForEnd ! null) {reverseLayout vectorForEnd.x <0.0F || vectorForEnd.y 0) {action.update(dx, dy, time, this.mDecelerateInterpolator);}}protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {return 100.0F / (float) displayMetrics.densityDpi;}protected int calculateTimeForScrolling(int dx) {return Math.min(100, super.calculateTimeForScrolling(dx));}};}private int distanceToStart(NonNull LayoutManager layoutManager, NonNull View targetView, OrientationHelper helper) {int childStart helper.getDecoratedStart(targetView);int containerStart;if (layoutManager.getClipToPadding()) {containerStart helper.getStartAfterPadding();} else {containerStart 0;}return childStart - containerStart;}Nullableprivate View findStartSnapView(LayoutManager layoutManager, OrientationHelper helper) {int childCount layoutManager.getChildCount();if (childCount 0) {return null;} else {View closestChild null;int start;if (layoutManager.getClipToPadding()) {start helper.getStartAfterPadding();} else {start 0;}int absClosest 2147483647;for (int i 0; i
//横向列表 3行GridLayoutManager gridLayoutManagernew GridLayoutManager(context, 3,LinearLayoutManager.HORIZONTAL, false);recycler.setLayoutManager(gridLayoutManager);homeAdapternew HomeAdapter();//3行1列new GridPagerSnapHelper(3,1).attachToRecyclerView(recycler);recycler.setAdapter(homeAdapter);
【文章原创作者:香港显卡服务器 http://www.558idc.com/hkgpu.html 网络转载请说明出处】