-1

I want to make a RecyclerView that scrolls infinitely while also being able to scroll to an item programmatically.

At the moment I've made the RecyclerView loop infinitely using this hacky method: https://stackoverflow.com/a/31254146/7443375

i.e. Overriding my adapter

@Override
public int getCount() {
    return Integer.MAX_VALUE;
}

Getting position of item in my adapter like so:

    int positionInList = position % fragmentList.size();

And then initializing my RecyclerView's scroll position like so:

recyclerView.getLayoutManager().scrollToPosition(Integer.MAX_VALUE / 2);

However, in my Fragment that has the RecyclerView, I want to be able to scroll to a specific item in my list (i.e. item 3 out of a list of 10 items). When I call

recyclerView.getLayoutManager().scrollToPosition(2);

The list goes into an infinite scroll. I can't figure out how to go to the specific item itself.

Furthermore, how can I make sure that the specific item is centered in the screen? I am using LinearSnapHelper to snap the items in the center of the screen as I scroll, but LinearSnapHelper does not seem to work when setting positions programmatically.

Cœur
  • 37,241
  • 25
  • 195
  • 267
damememan
  • 592
  • 7
  • 14
  • Provide some more code please, specially if you hace onScroll Listeners. Inifinite scrolling is little Mysterious in your case – Mohammed Atif Mar 16 '17 at 12:42
  • @MohammedAtif I don't have any more code related to the RecyclerView; on a button click I simply want to scroll to a specified position in the list (i.e. scrollToPosition(2)). I think what's happening is the recyclerview position is set at Integer.MAX_VALUE/2 and scrollToPosition(2) causes the RecyclerView to try to scroll all the way back to position 2 – damememan Mar 16 '17 at 12:49

1 Answers1

1

Try this way..

    recyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                                new CountDownTimer(Integer.MAX_VALUE, 20 ) {
                                    public void onTick(long millis) {
                                        recyclerView.scrollBy(0, 20);
                                    }

                                public void onFinish() {

                                }
                            }.start();
                        }
                    });
Jd Prajapati
  • 1,953
  • 13
  • 24
  • I'm not quite sure what this code does. Does the recyclerview atrbitrarily get scrolled by 20 pixels every 20ms? – damememan Mar 16 '17 at 12:57
  • yes, its auto scrolling every 20ms and by 20 px height – Jd Prajapati Mar 16 '17 at 12:58
  • How does this help me accomplish scrolling to a specific position? It seems to be scrolling infinitely. – damememan Mar 16 '17 at 13:01
  • what is your question? you want infinitely scrolling – Jd Prajapati Mar 16 '17 at 13:10
  • I want to be able to have the recyclerview be never-ending (i.e. when I get to the start or end of the list, the list loops) and also be able to scroll to a certain item in the recyclerview through code (i.e. call smoothScrollToPosition(3)) – damememan Mar 16 '17 at 13:37