17

I have a horizontal RecyclerView and two button (Next,Previous) as shown in the image below.

enter image description here

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

Ahmad Alkhatib
  • 1,230
  • 2
  • 14
  • 31

5 Answers5

34

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;
Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
Ahmad Alkhatib
  • 1,230
  • 2
  • 14
  • 31
23

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.

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
  • 2
    First, how do u expect anyone to help you if your not willing to even try first, second, if something is unclear u can ask for an elaboration. Simply writing "more code please?!?" Is quite rude and demanding, considering that some1 is taking the time to helo you out for baaically nothing in exchange – Gil Moshayof Aug 17 '15 at 06:23
  • @SoliTawako I guess `scrollTo(int x, int y)` you can also scroll view, but in a more fine-tuned way. – János Jul 06 '16 at 12:48
  • Is their any way to apply 'smoothScrollToPosition' over Layout Manager alos b/c if I applied in on recyclerView then it's not worked continuous (means stop after 1-2 scroll), while with recyclerView Layout Manager it's works fine on every time. @AhmadAlkhateeb – Ankit Kumar Singh Sep 08 '16 at 07:29
3
case R.id.next:
    mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findLastVisibleItemPosition() + 1);
    break;

case R.id.pre:
    mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findFirstVisibleItemPosition() - 1);
    break;
Yusuf Tarık Günaydın
  • 3,016
  • 2
  • 27
  • 41
eeeeee
  • 31
  • 1
3
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());
    }
});
Imanuel
  • 3,596
  • 4
  • 24
  • 46
0

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)
        }
    }
Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31