From 5fe9af78f9540a3cafaa2c7b2ac33e43e922b2d6 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sat, 12 Sep 2020 20:23:35 +0300 Subject: [PATCH] TopicActivity fix (propagation of touch events to Viewholders) --- .../latestPosts/LatestPostsAdapter.java | 1 - .../mthmmy/views/CustomRecyclerView.java | 5 +- .../mthmmy/views/ScrollAwareRecyclerView.java | 48 +++++++++++++++++++ app/src/main/res/layout/activity_topic.xml | 2 +- 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/views/ScrollAwareRecyclerView.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java index 3e82c5df..b052dc32 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java @@ -19,7 +19,6 @@ import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.PostSummary; import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.views.ReactiveWebView; -import timber.log.Timber; /** * {@link RecyclerView.Adapter} that can display a {@link TopicSummary} and makes a call to the diff --git a/app/src/main/java/gr/thmmy/mthmmy/views/CustomRecyclerView.java b/app/src/main/java/gr/thmmy/mthmmy/views/CustomRecyclerView.java index 1b605cdd..964eaa81 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/views/CustomRecyclerView.java +++ b/app/src/main/java/gr/thmmy/mthmmy/views/CustomRecyclerView.java @@ -5,10 +5,9 @@ import android.util.AttributeSet; import androidx.annotation.Nullable; import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; //Custom RecyclerView, so EdgeEffect and SwipeRefresh both work -public class CustomRecyclerView extends RecyclerView { +public class CustomRecyclerView extends ScrollAwareRecyclerView { private volatile boolean enableRefreshing = true; public CustomRecyclerView(Context context) { @@ -40,7 +39,6 @@ public class CustomRecyclerView extends RecyclerView { else if (((LinearLayoutManager) getLayoutManager()).findFirstCompletelyVisibleItemPosition() != 0) enableRefreshing = false; - super.onScrollStateChanged(state); } @@ -51,5 +49,4 @@ public class CustomRecyclerView extends RecyclerView { else return super.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, 0, offsetInWindow); } - } diff --git a/app/src/main/java/gr/thmmy/mthmmy/views/ScrollAwareRecyclerView.java b/app/src/main/java/gr/thmmy/mthmmy/views/ScrollAwareRecyclerView.java new file mode 100644 index 00000000..ee11a673 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/views/ScrollAwareRecyclerView.java @@ -0,0 +1,48 @@ +package gr.thmmy.mthmmy.views; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.MotionEvent; + +import androidx.recyclerview.widget.RecyclerView; + +// Based on https://stackoverflow.com/a/46453825 +// Not needed in LatestPostsFragment as there are no other ScrollAware Behaviors to steal touch events +public class ScrollAwareRecyclerView extends RecyclerView { + public ScrollAwareRecyclerView(Context context) { + super(context); + } + + public ScrollAwareRecyclerView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public ScrollAwareRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent event) { + final int scrollState = getScrollState(); + boolean requestCancelDisallowInterceptTouchEvent + = ((scrollState == SCROLL_STATE_SETTLING) || (scrollState == SCROLL_STATE_IDLE)); + boolean consumed = super.onInterceptTouchEvent(event); + final int action = event.getActionMasked(); + + if (action == MotionEvent.ACTION_DOWN) { + if (requestCancelDisallowInterceptTouchEvent) { + getParent().requestDisallowInterceptTouchEvent(false); + + // only if it touched the top or the bottom + if (!canScrollVertically(-1) || !canScrollVertically(1)) { + // stop scroll to enable child view to get the touch event + stopScroll(); + // do not consume the event + return false; + } + } + } + return consumed; + } + +} diff --git a/app/src/main/res/layout/activity_topic.xml b/app/src/main/res/layout/activity_topic.xml index 24793374..69fb3954 100644 --- a/app/src/main/res/layout/activity_topic.xml +++ b/app/src/main/res/layout/activity_topic.xml @@ -37,7 +37,7 @@ -