• RecyclerView 分页滑动,设置可以滑动下一页,下一个item作为起点方法


    mStartSnapHelper.attachToRecyclerView(mRecyclerViewMovie);
    mStartSnapHelper.setPageListener(this);
    mRecyclerViewMovie.setLayoutManager(mVLinearLayoutManager);


    public class StartSnapHelper extends PagerSnapHelper {
    private static final String TAG = AppConstants.APP_TAG + "StartSnapHelper ";
    private OrientationHelper mHorizontalHelper, mVerticalHelper;
    private int mCurrentPosition = 0;
    private int mLastPosition = 0;
    private PageListener mPageListener;

    public StartSnapHelper() {
    mCurrentPosition = 0;
    mLastPosition = 0;
    }

    public interface PageListener {
    void onPageSnapChanged();
    }

    public void setPageListener(PageListener pageListener) {
    mPageListener = pageListener;
    }

    @Override
    public int[] calculateDistanceToFinalSnap(RecyclerView.LayoutManager layoutManager, View targetView) {
    int[] out = new int[2];
    if (layoutManager.canScrollHorizontally()) {
    out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager));
    } else {
    out[0] = 0;
    }
    if (layoutManager.canScrollVertically()) {
    out[1] = distanceToStart(targetView, getVerticalHelper(layoutManager));
    } else {
    out[1] = 0;
    }
    return out;
    }

    private int distanceToStart(View targetView, OrientationHelper helper) {
    return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
    }

    @Override
    public View findSnapView(RecyclerView.LayoutManager layoutManager) {

    View view = super.findSnapView(layoutManager);
    if (null != view) {
    mCurrentPosition = layoutManager.getPosition(view);
    //LogUtil.i(TAG + "findSnapView()22 tag: " + view.getTag()
    // + " mCurrentPosition:" + mCurrentPosition + " mLastPosition:" + mLastPosition+" mPageListener:"+mPageListener);
    if (mLastPosition != mCurrentPosition && null != mPageListener) {
    mPageListener.onPageSnapChanged();
    }
    mLastPosition = mCurrentPosition;
    }

    if (layoutManager instanceof LinearLayoutManager) {

    if (layoutManager.canScrollHorizontally()) {
    return findStartView(layoutManager, getHorizontalHelper(layoutManager));
    } else {
    return findStartView(layoutManager, getVerticalHelper(layoutManager));
    }
    }
    return super.findSnapView(layoutManager);
    }


    private View findStartView(RecyclerView.LayoutManager layoutManager,
    OrientationHelper helper) {
    if (layoutManager instanceof LinearLayoutManager) {
    int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
    //check if last item, if last item should not align, otherwise it maybe show incompletely
    boolean isLastItem = ((LinearLayoutManager) layoutManager)
    .findLastCompletelyVisibleItemPosition()
    == layoutManager.getItemCount() - 1;

    if (firstChild == RecyclerView.NO_POSITION || isLastItem) {
    return null;
    }

    View child = layoutManager.findViewByPosition(firstChild);

    if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
    && helper.getDecoratedEnd(child) > 0) {
    return child;
    } else {
    if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()
    == layoutManager.getItemCount() - 1) {
    return null;
    } else {
    return layoutManager.findViewByPosition(firstChild + 1);
    }
    }
    }

    return super.findSnapView(layoutManager);
    }

    private OrientationHelper getHorizontalHelper(
    RecyclerView.LayoutManager layoutManager) {
    if (mHorizontalHelper == null) {
    mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
    }
    return mHorizontalHelper;
    }

    private OrientationHelper getVerticalHelper(RecyclerView.LayoutManager layoutManager) {
    if (mVerticalHelper == null) {
    mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager);
    }
    return mVerticalHelper;
    }
    }
  • 相关阅读:
    摩托罗拉挪动诉TiVo数字录像手艺专利侵权
    动静称Verizon三月末推出WP7手机
    2月25日中国观点股全线下跌 优酷网涨7.06%
    Verizon CEO称C版iPhone销售微弱
    外来往戏出海案例:热酷日本掘金之路
    摩根大通旗下基金或以4.5亿美元入股Twitter
    TCL通讯2010年净利润6.11亿元 同比增200%
    马克·扎克伯格和Facebook星球
    2月25日中国观点股评级:维持网易买入评级
    美媒评全球十家增速最快IT办事公司 当当网居首
  • 原文地址:https://www.cnblogs.com/adamli/p/14154001.html
Copyright © 2020-2023  润新知