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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…