首页 > 解决方案 > 如何实现recyclerview的水平自动滚动

问题描述

我正在尝试在 android 中实现自定义线性布局管理器。用于获得水平自动滑动回收器视图。但是当我尝试将我的自定义类调用到我的主 java 类中时,我遇到了一些问题。

下面列出了我的代码面临的问题。

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

我得到的错误是:从未使用构造函数'CustomLinearLayoutManager(android.content.Context,int,boolean)'

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

我得到的错误是:Constructor 'CustomLinearLayoutManager(android.content.Context, android.util.AttributeSet, int, int)' is never used

customLinearLayoutManager.smoothScrollToPosition();

我在这条线上面临的一个错误是:

CustomLinearLayoutManager 中的 smoothScrollToPosition() 不能应用于:预期参数:实际参数:recyclerView:RecyclerView 状态:状态位置:int

自定义 Java 类

public class CustomLinearLayoutManager extends LinearLayoutManager {

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

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

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

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        final LinearSmoothScroller linearSmoothScroller =
                new LinearSmoothScroller(recyclerView.getContext()) {
                    private static final float MILLISECONDS_PER_INCH = 200f;

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

                    @Override
                    protected float calculateSpeedPerPixel
                            (DisplayMetrics displayMetrics) {
                        return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
                    }
                };
        linearSmoothScroller.setTargetPosition(position);
        startSmoothScroll(linearSmoothScroller);
    }
}

主要Java类:

CustomLinearLayoutManager customLinearLayoutManager = new CustomLinearLayoutManager(getContext());
        customLinearLayoutManager.smoothScrollToPosition();
            recyclerViewHeaderSlider = view.findViewById(R.id.bannerSlider);
            SnapHelper snapHelper = new PagerSnapHelper();
            snapHelper.attachToRecyclerView(recyclerViewHeaderSlider);
            recyclerViewHeaderSlider.setHasFixedSize(true);
            recyclerViewHeaderSlider.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
            headerSliderAdapter.setOnClick(this);
            recyclerViewHeaderSlider.setAdapter(headerSliderAdapter);

请告知上述错误的解决方法。另外,请精确代码以实现水平自动滑动回收器视图。使用我提到的自定义线性布局管理器。

标签: javaandroidandroid-recyclerview

解决方案


为什么不使用正常的线性布局管理器并将方向设置为水平


推荐阅读