RecyclerView smoothScroll位于中心位置.安卓

Sta*_*olm 24 android android-recyclerview linearlayoutmanager

我正在使用水平布局管理器RecyclerView.我需要以下RecyclerView一种方式制作:当点击某个项目时 - 将smoothScrool放到该位置并将该项目放在中心RecyclerView(如果可能的话,例如20中的10项).

所以,我没有问题smoothScrollToPosition(),但如何把项目放在RecyclerView??? 的中心?

谢谢!

小智 69

是的,这是可能的.

通过实施RecyclerView.SmoothScroller的方法onTargetFound(View, State, Action).

/**
 * Called when the target position is laid out. This is the last callback SmoothScroller
 * will receive and it should update the provided {@link Action} to define the scroll
 * details towards the target view.
 * @param targetView    The view element which render the target position.
 * @param state         Transient state of RecyclerView
 * @param action        Action instance that you should update to define final scroll action
 *                      towards the targetView
 */
abstract protected void onTargetFound(View targetView, State state, Action action);
Run Code Online (Sandbox Code Playgroud)

特别是在LinearLayoutManagerLinearSmoothScroller:

public class CenterLayoutManager extends LinearLayoutManager {

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

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

    public CenterLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

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

    private static class CenterSmoothScroller extends LinearSmoothScroller {

        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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • `onTargetFound()`函数在哪里实现? (5认同)
  • 这不适用于最后和第一个职位 (5认同)
  • 完美,对于其他人,你只需要调用`recyclelerView.smoothScrollToPosition(position);`并且不要忘记设置布局管理器`recyclerView.setLayoutManager(layoutManager);` (4认同)
  • 但是我没理解为什么你提到这个方法`onTargetFound()`? (4认同)
  • 扩展LinearSmoothScroller必须覆盖computeScrollVectorForPosition() (3认同)

小智 18

答案的改进-无需覆盖LinearLayoutManager

从先前的答案:

public class CenterSmoothScroller extends LinearSmoothScroller {

    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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这里如何使用它:

RecyclerView.LayoutManager lm = new GridLayoutManager(...): // or whatever layout manager you need

...

RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());

smoothScroller.setTargetPosition(position);

lm.startSmoothScroll(smoothScroller);
Run Code Online (Sandbox Code Playgroud)

  • 对于像我一样感到困惑的其他人,“CenterSmoothScroller”是在接受的答案中找到的一个类。 (2认同)

小智 14

以防万一有人需要在接受的答案中与该课程等效Kotlin

class CenterLayoutManager : LinearLayoutManager {
    constructor(context: Context) : super(context)
    constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)

    override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State, position: Int) {
        val centerSmoothScroller = CenterSmoothScroller(recyclerView.context)
        centerSmoothScroller.targetPosition = position
        startSmoothScroll(centerSmoothScroller)
    }

    private class CenterSmoothScroller(context: Context) : LinearSmoothScroller(context) {
        override fun calculateDtToFit(viewStart: Int, viewEnd: Int, boxStart: Int, boxEnd: Int, snapPreference: Int): Int = (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2)
    }
}
Run Code Online (Sandbox Code Playgroud)