我的活动中有一个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);
答案 0 :(得分:0)
尝试以下逻辑:
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);
}
});
答案 1 :(得分:0)
尝试使用此代码段实现对我有用:
RecyclerView my_recycler_view= (RecyclerView) findViewById(R.id.my_recycler_view);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(my_recycler_view);
享受编码活动的欢呼!
答案 2 :(得分:0)
感谢您的意见。我尝试使用线程应用某些逻辑,但我的应用不断崩溃。所以我找到了解决方案here和here also
首先创建可运行的文件:
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);
答案 3 :(得分:0)
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);