How to correct intercept scrolling gesture in child of horizontal RecyclerView

25 Views Asked by At

One part of my main screen in Android App is horizontal recycler view with 'products' elements. In each 'Product Item' view I have another view with several photos. And I want to user can scroll this photos. (note, that my colleague use for that 'photo carousel' ViewPager2 component to implement custom 'dots' logic) But as you can see on the gif - all what can I do - is only to scroll RW, not child view with photos of Product.

enter image description here

I've tried to use different approaches, but no one is worked for me:

  1. set android:nestedScrollingEnabled="true" for parent RecyclerView
  2. intercept touch event in child and check - does 'touch' motion lead on the child view visible rect. Some thoughts about this I've get from this un-answered question. My sample code of it is like this:
        view.recyclerViewProducts.setOnTouchListener { v, event ->
        val touchedView = view.rvProducts.findChildViewUnder(event.x, event.y) as? ViewGroup
            ?: return@setOnTouchListener false
    
        touchedView.children.forEach { childView ->
            // find FrameLayout (R.id.images),  inside which ViewPager2 is 
            if (childView is FrameLayout) {
                childView.children.forEach { nestedChildView ->
                    if (nestedChildView is ViewPager2 &&
                        childView.isViewInBounds(event.x.roundToInt(), event.y.roundToInt())
                    ) {
                        touchedView.onTouchEvent(event)
                        return@setOnTouchListener true
                    }
                }
            }
        }
 
         return@setOnTouchListener false
    }
    
    private fun View.isViewInBounds(x: Int, y: Int): Boolean {
        val outRect = Rect()
        val location = IntArray(2)
        getDrawingRect(outRect)
        getLocationOnScreen(location)
        outRect.offset(location[0], location[1])
        return outRect.contains(x, y)
    } 

But I'm always get false from function isViewInBounds. My thoughts about this - that my calculation of offset is wrong. But I don't know is it true, or I have some another mistake.

Or maybe someone can show example how to deal with such situation - when I need to enable for user nested horizontal scrolling inside horizontal RecyclerView.

0

There are 0 best solutions below