3

Description: I am working on one demo with the functionality that user can select an option from the item of horizontal Recyclerview. In this, I got stuck in below queries.

Queries:

1: How to make Recyclerview cyclic?

For example, I have 10 items named 1,2,3,4,5,6,7,8,9,10. Now if user scrolls the recyclerview then he/she will be able to scroll it endlessly. i.e 1,2,3,4,5...9,10,1,2...9,10,1,2,3..9,10 like this. To achieve this I have used this answer. Somehow it worked but only in forward direction.

2: How to give Snap center to the particular item which is selected by the user?

For example: As shown in the image, if the user clicks on '15' then that item will come to the center. Same as if the user clicks on '07' then it should come to the center position. I have implemented this demo. But it doesn't work on click on the item.

The code which I have done so far is as below:

Layout Manager:

final CenterZoomLayoutManager mLayoutManager = new CenterZoomLayoutManager(mContext,
            CenterZoomLayoutManager.HORIZONTAL, false);// To get centered item zoom
recyclerview.setLayoutManager(mLayoutManager);

Adapter object:

mAdapter = new CustomAdapter(mContext, arrayList, new RecyclerViewClickListener() {
        @Override
        public void recyclerViewListClicked(View v, int position) {

        }
    });
    recyclerview.setAdapter(mAdapter);

Any help is appreciated! Thanks.

Horizontal Recylerview in which user can select item as an option

Maddy
  • 4,525
  • 4
  • 33
  • 53
Ronak Thakkar
  • 2,515
  • 6
  • 31
  • 45

3 Answers3

0

CarouselView may fulfill your requirements. Please check following GitHub links for the same.

Kuls
  • 2,047
  • 21
  • 39
0

How do I create a circular (endless) RecyclerView?

There is no way of making it infinite, but there is a way to make it look like infinite.

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

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

in getItem() and getView() modulo divide (%) position by real item number:

@Override
public Fragment getItem(int position) {
    int positionInList = position % fragmentList.size();
    return fragmentList.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);

For snapping use SnapHelper or scrollToPositionWithOffset, where offset is (width/2 - itemWidth/2)

no_fate
  • 1,625
  • 14
  • 22
0

I can use this library You can solve your problem by personalizing this library. By selecting each item, you can use the ScrollToX function to navigate to that item.

https://github.com/mahdimoaz/RecyclerViewHorizontalCenterSelected