1

Currently I'm working on a recycler view that has a horizontal scroll.

I have a list that contains these values:

{"2001","2002","2003","2004","2005","2006","2007","2008","2009","2010",...}

and so on, until 2017

My objective here is to select the year on the center

All works good but I have 2 problems here :

  1. I can't scroll to the right edge, it's like the recyclerview has its limit on scrolling. Let's say the 4 last items are "2014", "2015", "2016", "2017". I can only scroll until "2003". For now i fix this issue by adding a dummy years before and after the list. (very hackish, but not so solid)

  2. I want to show the first child when the scrolling reaches the end. For example, when I'm able to scroll until the end 2017 , I want to show the first item of the list right next to 2017 and in this case is "2001". For example: 2013 | 2014 | 2015 | 2016 | 2017 | 2001 | ...

Webster
  • 1,113
  • 2
  • 19
  • 39

2 Answers2

1

what you should use here is loop your recyclerview

in your adapter override getItemCount() to return something big like Integer.MAX_VALUE:

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

in onBindViewHolder() modulo divide (%) position by real item number:

 @Override
   public void onBindViewHolder(MyViewHolder holder, int position) {
        int positionInList = position % dataList.size();
        Item item = dataList.get(positionInList);
        //...
    }

at the end, set current item to something in the middle (or else, it would be endless only in downward direction).

// scroll to middle item
recyclerView.getLayoutManager().scrollToPosition(Integer.MAX_VALUE / 2);
Boukharist
  • 1,219
  • 11
  • 19
0

Try this,

recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
recyclerView.getLayoutManager().scrollToPosition(5);

you can put your item number here in place of 5, can do (list.size()-3) for specific year.

nayana bhoj
  • 115
  • 1
  • 11