如何使recyclerview自动水平滚动?

时间:2019-08-29 13:32:40

标签: java android android-recyclerview

我在项目中放入了一个回收站视图。我希望它会水平自动滚动。为了实现这一点,我进行了一个自定义类。但我也希望我保留在回收者视图中的现有功能也将保留。

CustomLinearLayoutManager:

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);
    }
}

家庭班

private RecyclerView recyclerViewHeaderSlider;
    private HeaderSliderAdapter headerSliderAdapter;
    private List<Banner> banners;

banners = new ArrayList<>();
        headerSliderAdapter = new HeaderSliderAdapter(getActivity(), banners);

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);

我想用Home类实现自定义线性布局管理器。

1 个答案:

答案 0 :(得分:0)

您可以使用Runnable自动滚动水平RV

public void autoScroll(){
    speedScroll = 0;
    handler = new Handler();
    runnable = new Runnable() {
        int count = 0;
        @Override
        public void run() {
            if(count == tickerAdapter.getItemCount())
                count = 0;
            else {
                if(count < tickerAdapter.getItemCount()){
                    rvTicker.smoothScrollToPosition(++count);
                    handler.postDelayed(this,speedScroll);
                }else {
                    count = 0;
                }
            }
        }
    };
    handler.postDelayed(runnable,speedScroll);
}