Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

Custom SwipeToRefresh to move RecyclerView Android nested scroll

I'm trying to implement a custom swipe to refresh on RecyclerView with a custom view in order to move down the recycler with the refresh symbol.

I'm using onInterceptTouchEvent and onTouchEvent method inside custom view to handle the swipe.

 @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (!canSwipe) {
            return false;
        }
        else {
            switch (ev.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    isDown = true;
                    startY = ev.getRawY();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    dy = ev.getRawY() - startY;

                    if (dy < 0) {
                        canSwipe = false;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
            return true;
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!canSwipe) {
            return false;
        }
        else {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    isDown = true;
                    startY = event.getRawY();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    if (!isDown) {
                        return false;
                    }
                    dy = event.getRawY() - startY;

                    Log.d(TAG, "onTouchEvent: dy " + dy);
                    if (dy < 0) {
                         //canSwipe = false;
                    }
                    else {
                        onDrag(dy);
                    }

                    break;
                case MotionEvent.ACTION_UP:
                    if (!isDown) {
                        return false;
                    }
                    isDown = false;
                    canSwipe = false;

                    if (canBeRefreshed) {
                        canBeRefreshed = false;
                        onDrop();
                    }
                    break;
            }
            return true;
        }
    }

And the RecyclerView logic to delegate to custom view :

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if (dy < 0) {
                    if (layoutManager.findFirstVisibleItemPosition() != 0) {
                        swipeRefreshView.setCanSwipe(false);
                    }
                    // reached top, recycler delegate but need to touch again
                    else {
                        swipeRefreshView.setCanSwipe(true);
                    }
                }
                // scroll down
                else {
                    swipeRefreshView.setCanSwipe(false);
                }
            }
        });

Problem :

If the recycler view reach top, canSwipe is true then the parent handle the touch event.

Inside the custom view, on ACTION_MOVE I know that dy < 0 is a scroll down and not a refresh. I would like, on this moment, delegate the touch to the recycler.

I tried to set canSwipe to false inside this condition, which is completely stopping the touch instead delegate it to the recycler on the same touch event. I also tried to use requestDisallowInterceptTouchEvent() but no result.

How can I delegate the touch on the same event ? Any different approach to solve this is accepted !

question from:https://stackoverflow.com/questions/65944729/custom-swipetorefresh-to-move-recyclerview-android-nested-scroll

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...