0

In my vertical recyclerview, I have multiple horizontal recyclerviews and I have a problem: when I want to scroll left or right, if I'm scrolling a bit up or down at the same time, the horizontal scrolling doesn't work.

So my idea was to redefine what is up, down, left and right for the recycler view. So I stumbled upon this thread : How to detect swipe direction between left/right and up/down

First I though it was what I need but then I understood, it was just redefining directions at the end of a swipe (not during), so I tried to do all the angle computation during the swipe. I ended with this :

var initialX: Float
var initialY: Float

    ui_recyclerView?.setOnTouchListener(object: View.OnTouchListener{

            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                val action = event?.actionMasked

                if (action == MotionEvent.ACTION_DOWN) {
                    initialX = event.x
                    initialY = event.y

                }
                else if(action == MotionEvent.ACTION_MOVE) {
                    val swipeDirection = GestureUtils.getSwipeDirection(initialX, initialY, event.x, event.y)

                    if (swipeDirection == GestureUtils.Direction.UP || swipeDirection == GestureUtils.Direction.DOWN) {
                        return false
                    }

                }

                return true
            }

        })

It worked but then I didn't have the smooth scrolling any more (it was very robotic), and neither the click.

Is there a built-in function with recycler view to limit MoveEvent to a certain angle ? If not, how can I put back the smooth scrolling effect of a normal recycler view ?

That would help me a lot !

EtienneG
  • 13
  • 2

2 Answers2

0

You can try to limit your scroll vertical movements on your recycler with a ScrollListener, so it only triggers when you make a large scroll on the screen, like this:

final int delimitationUp=25;
final int delimitationDown=-25;
yourVerticalRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if(dy>delimitationUp){//move up} 
            if (dy<delimitationDown){//move down}

Test with the delimitations

  • Hi Alvaro, thank you for your answer, I've tried what you've said, there may be something but how can I stop the scroll when I'm out of the delimitations bound ? I've tried to write "super.onScrolled(recyclerView, 0,0)" but it didn't work – EtienneG Feb 19 '20 at 12:20
  • You can also try yourVerticalRecycler.setNestedScrollingEnabled(false); when onScrolled – Álvaro Briz Feb 19 '20 at 14:53
  • It is the exact opposite that I want :D I want my sub recycler views to be more easily scrollable – EtienneG Feb 19 '20 at 17:15
  • So, you just have to implement the OnScrollListener on the horizontal recycler chids, and call the yourVerticalRecycler.setNestedScrollingEnabled(false) when chid scrolls horizontally – Álvaro Briz Feb 19 '20 at 20:10
0
recyclerView.setNestedScrollingEnabled(false);
Wahdat Jan
  • 3,563
  • 3
  • 14
  • 37