avatarElye - A One Eye Developer

Summary

The article discusses a technique to improve the user experience when scrolling through a vertical RecyclerView that contains multiple horizontal RecyclerViews, ensuring smooth transitions between vertical and horizontal scrolling.

Abstract

The article addresses the challenge of implementing smooth scrolling behavior in an Android application, similar to the Google Play Store, where a vertical RecyclerView contains multiple horizontal RecyclerViews. The author notes that without proper handling, users may need to touch a horizontal RecyclerView twice to initiate scrolling, which is not user-friendly. The solution provided involves using the addOnItemTouchListener method to manage touch events, allowing for seamless scrolling in both directions without the need for multiple touches. This improvement is demonstrated with a code snippet and animated GIFs showing the enhanced scrolling behavior. The article credits a Stack Overflow discussion and a GitHub repository for providing insights into the solution, and it encourages readers to explore the author's other work for more Android development tips.

Opinions

  • The author believes that smooth cross-scrolling between vertical and horizontal RecyclerViews significantly enhances user friendliness.
  • Initially, the author considered managing touch events manually by overriding onInterceptTouchEvent, but found it overly complex.
  • The preferred solution utilizes addOnItemTouchListener to achieve the desired touch behavior, which is seen as a more effective approach.
  • The author acknowledges the difficulty in achieving this functionality and provides a link to a Stack Overflow discussion that contributed to the solution.
  • The article emphasizes the importance of user experience, suggesting that even small improvements in touch interactions can make a significant difference.
  • The author invites readers to follow their work on Medium, Twitter, and Facebook for more insights on Android and Kotlin development.

Smooth cross RecyclingViews Swipe

Not sure, if you realize that Google Playstore App is a vertical RecyclerView containing many horizontal RecyclerViews (or something of that nature).

The scroll between them is so smooth, where after one scroll vertically, a touch could stop the vertical scroll, and then scroll horizontally, as shown below.

But that is not what I experience…

However if one code such mixed RecyclerViews as below, it can’t scroll the horizontal RecyclerView on the first touch. It need to touch it again then only the horizontal RecyclerView could be scroll.

It’s not a big deal, but from a user friendliness point of view, such little feature would makes much different.

So how could we deal with it?

Solution

Initially I was thinking about managing the touch flow myself, using custom RecyclerView to override the onInterceptTouchEvent function as per the blog Understanding Android touch flow control.

However this prove to be super difficult, as if I implement that, the Horizontal RecyclerView could be swipe immediately, but then it will prevent the Vertical RecyclerView to be swiped when the Horizontal RecyclerView is touch.

Using addOnItemTouchListener

Later, discover that in RecyclerView, there’s a method added to intercept the touch behavior implemented in RecyclerView, by using the addOnItemTouchListener.

In short, in our RecyclerView, just add the ItemTouchListener as below, then the swipe will be possible for both Horizontal and Vertical RecyclerView

recycler_view.addOnItemTouchListener(
  object: RecyclerView.OnItemTouchListener {
    override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
    override fun onInterceptTouchEvent(rv: RecyclerView, e: 
            MotionEvent): Boolean {
        if (e.action == MotionEvent.ACTION_DOWN &&
            rv.scrollState == RecyclerView.SCROLL_STATE_SETTLING) {
            rv.stopScroll()
        }
        return false
    }
    override fun onRequestDisallowInterceptTouchEvent(
        disallowIntercept: Boolean) {}
})

The result looks like below, just a single touch on the Horizontal RecyclerView, could scroll it horizontally, without need to touch it again,

The answer should be credited to

To get the code, refers to

I hope this post is helpful to you. You could check out my other interesting topics here.

Follow me on medium, Twitter or Facebook for little tips and learning on Android, Kotlin etc related topics. ~Elye~

Android
Android App Development
AndroidDev
Mobile App Development
Google
Recommended from ReadMedium