Home > database >  How can I make recyclerview scroll by only one direction and disable others
How can I make recyclerview scroll by only one direction and disable others

Time:07-19

I just want to allow only scroll left in horizontal recyclerview for example. Please help me on that

CodePudding user response:

Best way is to remove other directions item from adapter, which are getting hidden one by one. So when user scroll back ward it will not show up any thing.

CodePudding user response:

I would not touch the data in your adapter (what does the data have to do with a UI business rule?). If you want, you could notify your Repository or ViewModel what views have been "scrolled" but that's a lot of events going up, and state coming down for everyone involved.

You have the tools at your disposal in the form of a combination of:

  1. A GestureDetector (See (the documentation.)
  2. A TouchListener (See (the documentation.)
  3. Your favorite search engine to put these things together.

or perhaps benefit from the somewhat modular approach of RecyclerView:

Using OnItemTouchListener and a OnScrollListener to analyze the events and ignore the ones you don't want.

You could also create your own extended RecyclerView but that's more painful because you now need to change it all over the place you want this behavior.

In any case, your search engine of choice is also a good starting point. But think of this as:

You will need to intercept the touches and flings gestures (as well as other motion actions like "Dpads" and other Accessibility Events that may trigger a "scroll", evaluate in which direction this is headed, and determine if you still want that event to happen.

  • Related