I have a horizontal RecyclerView
and two button (Next,Previous) as shown in the image below.
so i need to move to the next item or position by use these buttons , i know about method called scrollTo
but i don't know how does it work
I have a horizontal RecyclerView
and two button (Next,Previous) as shown in the image below.
so i need to move to the next item or position by use these buttons , i know about method called scrollTo
but i don't know how does it work
I found the answer:
case R.id.next:
mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findLastVisibleItemPosition() + 1);
break;
case R.id.pre:
mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findFirstVisibleItemPosition() - 1);
break;
RecyclerViews
have a methods which they expose for scrolling to a certain position:
Snap scroll to a given position:
mRecyclerView.scrollToPosition(int position)
Smooth scroll to a given position:
mRecyclerView.smoothScrollToPosition(int position)
For these methods to work, the LayoutManager
of the RecyclerView
needs to have implemented these methods, and LinearLayoutManager
does implement these in a basic manner, so you should be good to go.
case R.id.next:
mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findLastVisibleItemPosition() + 1);
break;
case R.id.pre:
mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findFirstVisibleItemPosition() - 1);
break;
int mFirst=0, mLast=0;
recyclerview.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager llm = (LinearLayoutManager) recyclerview.getLayoutManager();
mLast = llm.findLastCompletelyVisibleItemPosition();
mFirst = llm.findFirstCompletelyVisibleItemPosition();
}
});
imgRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayoutManager llm = (LinearLayoutManager) recyclerview.getLayoutManager();
llm.scrollToPositionWithOffset(mLast + 1, List.length());
}
});
imgLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayoutManager llm = (LinearLayoutManager) recyclerview.getLayoutManager();
llm.scrollToPositionWithOffset(mFirst - 1, List.length());
}
});
Adding to the accepted answer you need to have a check for the present position so that you don't get a nullPointerException like below
binding.imageViewleft.setOnClickListener {
if (layoutManager.findFirstCompletelyVisibleItemPosition()>0){
binding.recyclerView.layoutManager?.scrollToPosition(layoutManager.findFirstCompletelyVisibleItemPosition()-1)
}
}
binding.iv2.setOnClickListener {
if (layoutManager.findLastCompletelyVisibleItemPosition()<(sectiondata.size-1) ){
binding.recyclerView.layoutManager?.scrollToPosition(layoutManager.findLastCompletelyVisibleItemPosition()+1)
}
}