From d9f001d9578593b12decb1c4aec07bbfed15b944 Mon Sep 17 00:00:00 2001 From: ruben Date: Sat, 17 Aug 2019 13:26:44 +0100 Subject: [PATCH] Don't depend on LinearLayoutManager --- .../OrientationAwareRecyclerView.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/gravitysnaphelper/src/main/java/com/github/rubensousa/gravitysnaphelper/OrientationAwareRecyclerView.java b/gravitysnaphelper/src/main/java/com/github/rubensousa/gravitysnaphelper/OrientationAwareRecyclerView.java index 83dfb84..79b5d28 100644 --- a/gravitysnaphelper/src/main/java/com/github/rubensousa/gravitysnaphelper/OrientationAwareRecyclerView.java +++ b/gravitysnaphelper/src/main/java/com/github/rubensousa/gravitysnaphelper/OrientationAwareRecyclerView.java @@ -22,7 +22,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; /** @@ -33,7 +32,6 @@ public class OrientationAwareRecyclerView extends RecyclerView { private float lastX = 0.0f; private float lastY = 0.0f; - private int orientation = RecyclerView.VERTICAL; private boolean scrolling = false; public OrientationAwareRecyclerView(@NonNull Context context) { @@ -57,15 +55,12 @@ public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newStat } @Override - public void setLayoutManager(@Nullable LayoutManager layout) { - super.setLayoutManager(layout); - if (layout instanceof LinearLayoutManager) { - orientation = ((LinearLayoutManager) layout).getOrientation(); + public boolean onInterceptTouchEvent(MotionEvent e) { + final LayoutManager lm = getLayoutManager(); + if (lm == null) { + return super.onInterceptTouchEvent(e); } - } - @Override - public boolean onInterceptTouchEvent(MotionEvent e) { boolean allowScroll = true; switch (e.getActionMasked()) { @@ -87,8 +82,7 @@ public boolean onInterceptTouchEvent(MotionEvent e) { float currentY = e.getY(); float dx = Math.abs(currentX - lastX); float dy = Math.abs(currentY - lastY); - allowScroll = dy > dx ? orientation == RecyclerView.VERTICAL - : orientation == RecyclerView.HORIZONTAL; + allowScroll = dy > dx ? lm.canScrollVertically() : lm.canScrollHorizontally(); break; } }