Reputation: 101
I have a RecyclerView
in my activity. Can I programmatically scroll my RecyclerView
items one at a time (like the animation effect on a carousel) ? I have some code below but i don’t know how to make the effect. Any suggestions are welcomed!
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);
Upvotes: 1
Views: 6195
Reputation: 598
This answer given here works really well. I have used it many times and didn't get an error. In both simple and hassle free.
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);
Upvotes: 1
Reputation: 101
Thanks for help in the comments. I try applying some of the logic using a Thread but my app keep crashing. So i found my solution here and here also
first create 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);
}
};
then after setadapter() to the recyclerView use following:
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);
Upvotes: 3
Reputation: 4504
Try this logic:
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);
}
});
Upvotes: 0
Reputation: 523
Try implementing using this snippet works for me:
RecyclerView my_recycler_view= (RecyclerView) findViewById(R.id.my_recycler_view);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(my_recycler_view);
Enjoy your coding activities cheers!
Upvotes: 0