欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

RecyclerView选中项居中(横向、竖向),指定位置置顶(竖向)

程序员文章站 2022-03-08 15:45:04
需要实现对LinearLayoutManager的重写中间显示类import android.content.Context;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.LinearSmoothScroller;import android.support.v7.widget.RecyclerView;import android.util.DisplayMet...

需要实现对LinearLayoutManager的重写

中间显示类


import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;

public class CenterLayoutManager extends LinearLayoutManager {

    static int lastPositon = 0;
    static int targetPosion = 0;

    public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        CenterSmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }

    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int lastpositon, int position) {
        this.lastPositon = lastpositon;
        this.targetPosion = position;
        smoothScrollToPosition(recyclerView, state, position);
    }

    public static class CenterSmoothScroller extends LinearSmoothScroller {

        private static float duration = 800f;//显示到中间的动画时长

        public CenterSmoothScroller(Context context) {
            super(context);
        }

        @Override
        public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
            return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            float newDuration = duration / (Math.abs(targetPosion - lastPositon));//重新计算相近两个位置的滚动间隔
            return newDuration / displayMetrics.densityDpi;
        }

        @Override
        protected int calculateTimeForScrolling(int dx) {
            return super.calculateTimeForScrolling(dx);
        }

    }
}

置顶显示类


import android.content.Context;
import android.graphics.PointF;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;

public class TopLinearLayoutManager extends LinearLayoutManager {

    public TopLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
                                       int position) {
        RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }

    private class TopSnappedSmoothScroller extends LinearSmoothScroller {
        public TopSnappedSmoothScroller(Context context) {
            super(context);

        }

        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return TopLinearLayoutManager.this
                    .computeScrollVectorForPosition(targetPosition);
        }

        @Override
        protected int getVerticalSnapPreference() {
            return SNAP_TO_START;
        }
    }
}

调用方法,按需采用

//初始化置顶
TopLinearLayoutManager topLinearLayoutManager = new TopLinearLayoutManager(getContext(), 
LinearLayoutManager.VERTICAL, false);
//初始化中间竖向
CenterLayoutManager centerLayoutManager = new CenterLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
//初始化中间横向
CenterLayoutManager centerLayoutManager = new CenterLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
//绑定置顶
mRecyclerView.setLayoutManager(topLinearLayoutManager);
//绑定中间
mRecyclerView.setLayoutManager(centerLayoutManager );

//置顶显示调用
mRecyclerView.smoothScrollToPosition(position);
//中间显示调用 lastPostion是临时保存的目前处在中间的position
centerLayoutManager.smoothScrollToPosition(mRecyclerView, new RecyclerView.State(), lastPostion, position);
        //记住当前选中的位置作为下一次选中的位置的上一次位置
        if (lastPostion != position)
            lastPostion = position;

 

本文地址:https://blog.csdn.net/u011652925/article/details/110957247

相关标签: 开发代码