RecyclerView 自动滚动项目一项一项

RecyclerView auto scroll item one by one

我的 activity 中有一个 RecyclerView。我能否以编程方式一次滚动我的 RecyclerView 项(如旋转木马上的动画效果)?我在下面有一些代码,但我不知道如何制作效果。欢迎提出任何建议!

RandomProductAdapter randomProductAdapter = new RandomProductAdapter(this.mContext, randomProductList);
random_rv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
random_rv.setAdapter(randomProductAdapter);

random_rv.smoothScrollToPosition(randomProductAdapter.getItemCount()-1);

试试这个逻辑:

findViewById(R.id.iv_next).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int totalItemCount = recycler_list.getAdapter().getItemCount();
        if (totalItemCount <= 0) return;
        int lastVisibleItemIndex = layoutManager.findLastVisibleItemPosition();
        if (lastVisibleItemIndex >= totalItemCount) return;
        layoutManager.smoothScrollToPosition(rv_category_list, null, lastVisibleItemIndex + 1);
    }
});

尝试使用适合我的代码片段来实施:

RecyclerView my_recycler_view= (RecyclerView) findViewById(R.id.my_recycler_view);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(my_recycler_view);

享受你的编码活动干杯!

感谢您在评论中的帮助。我尝试使用 Thread 应用一些逻辑,但我的应用程序不断崩溃。所以我找到了我的解决方案 here and here also

首先创建runnable:

final int duration = 10;
final int pixelsToMove = 30;
private final Handler mHandler = new Handler(Looper.getMainLooper());
private final Runnable SCROLLING_RUNNABLE = new Runnable() {

    @Override
    public void run() {
        recyclerView.smoothScrollBy(pixelsToMove, 0);
        mHandler.postDelayed(this, duration);
    }
};

然后在 setadapter() 到 recyclerView 之后使用以下内容:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int lastItem = layoutManager.findLastCompletelyVisibleItemPosition();
            if(lastItem == layoutManager.getItemCount()-1){
                mHandler.removeCallbacks(SCROLLING_RUNNABLE);
                Handler postHandler = new Handler();
                postHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.setAdapter(null);
                        recyclerView.setAdapter(madapter);
                        mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
                    }
                }, 2000);
            }
        }
    });
    mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);

This 这里给出的答案非常有效。我已经使用了很多次并且没有出错。既简单又无忧。

final int time = 4000; // it's the delay time for sliding between items in recyclerview
final Adapter adapter = new Adapter(dataItems);
recyclerView.setAdapter(adapter);

final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(rootView.getContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);

//The LinearSnapHelper will snap the center of the target child view to the center of the attached RecyclerView , it's optional if you want , you can use it
final LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
linearSnapHelper.attachToRecyclerView(recyclerView); 

final Timer timer = new Timer();
timer.schedule(new TimerTask() {

    @Override
    public void run() {

        if (linearLayoutManager.findLastCompletelyVisibleItemPosition() < (adapter.getItemCount() - 1)) {
            linearLayoutManager.smoothScrollToPosition(recyclerView, new RecyclerView.State(), linearLayoutManager.findLastCompletelyVisibleItemPosition() + 1);
        }

        else if (linearLayoutManager.findLastCompletelyVisibleItemPosition() == (adapter.getItemCount() - 1)) {
            linearLayoutManager.smoothScrollToPosition(recyclerView, new RecyclerView.State(), 0);
        }
    }
}, 0, time);