From 27152bf1d5f2f329d7afd578073fc718215957c2 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Mon, 28 Oct 2024 12:28:26 -0400 Subject: Refine TableHorizontalScrollView MotionEvent processing. --- .../customviews/TableHorinzontalScrollView.java | 112 ------------------ .../customviews/TableHorizontalScrollView.java | 130 +++++++++++++++++++++ app/src/main/res/layout/adapter_table_block.xml | 4 +- 3 files changed, 132 insertions(+), 114 deletions(-) delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/customviews/TableHorinzontalScrollView.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/customviews/TableHorizontalScrollView.java diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/TableHorinzontalScrollView.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/TableHorinzontalScrollView.java deleted file mode 100644 index 6d8615cf..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/TableHorinzontalScrollView.java +++ /dev/null @@ -1,112 +0,0 @@ -package ml.docilealligator.infinityforreddit.customviews; - -import android.content.Context; -import android.os.Handler; -import android.os.Looper; -import android.util.AttributeSet; -import android.view.MotionEvent; -import android.view.ViewParent; -import android.widget.HorizontalScrollView; - -import androidx.viewpager2.widget.ViewPager2; - -import ml.docilealligator.infinityforreddit.customviews.slidr.widget.SliderPanel; - -public class TableHorinzontalScrollView extends HorizontalScrollView { - private CustomToroContainer toroContainer; - private ViewPager2 viewPager2; - private SliderPanel sliderPanel; - - private float lastX = 0.0f; - private float lastY = 0.0f; - - public TableHorinzontalScrollView(Context context) { - super(context); - init(); - } - - public TableHorinzontalScrollView(Context context, AttributeSet attrs) { - super(context, attrs); - init(); - } - - public TableHorinzontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - init(); - } - - public TableHorinzontalScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); - init(); - } - - private void init() { - new Handler(Looper.getMainLooper()).post(() -> { - ViewParent parent = getParent(); - while (parent != null) { - if (parent instanceof CustomToroContainer) { - toroContainer = (CustomToroContainer) parent; - } else if (parent instanceof ViewPager2) { - viewPager2 = (ViewPager2) parent; - } else if (parent instanceof SliderPanel) { - sliderPanel = (SliderPanel) parent; - } - - parent = parent.getParent(); - } - }); - } - - @Override - public boolean onInterceptTouchEvent(MotionEvent ev) { - boolean allowScroll = true; - - switch (ev.getActionMasked()) { - case MotionEvent.ACTION_DOWN: - lastX = ev.getX(); - lastY = ev.getY(); - - if (toroContainer != null) { - toroContainer.requestDisallowInterceptTouchEvent(true); - } - if (viewPager2 != null) { - viewPager2.setUserInputEnabled(false); - } - if (sliderPanel != null) { - sliderPanel.requestDisallowInterceptTouchEvent(true); - } - break; - case MotionEvent.ACTION_UP: - if (toroContainer != null) { - toroContainer.requestDisallowInterceptTouchEvent(false); - } - if (viewPager2 != null) { - viewPager2.setUserInputEnabled(true); - } - if (sliderPanel != null) { - sliderPanel.requestDisallowInterceptTouchEvent(false); - } - break; - case MotionEvent.ACTION_MOVE: - float currentX = ev.getX(); - float currentY = ev.getY(); - float dx = Math.abs(currentX - lastX); - float dy = Math.abs(currentY - lastY); - - allowScroll = dy < dx; - - if (toroContainer != null) { - toroContainer.requestDisallowInterceptTouchEvent(allowScroll); - } - if (viewPager2 != null) { - viewPager2.setUserInputEnabled(false); - } - if (sliderPanel != null) { - sliderPanel.requestDisallowInterceptTouchEvent(true); - } - break; - } - - return allowScroll && super.onInterceptTouchEvent(ev); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/TableHorizontalScrollView.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/TableHorizontalScrollView.java new file mode 100644 index 00000000..e5687a4c --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/TableHorizontalScrollView.java @@ -0,0 +1,130 @@ +package ml.docilealligator.infinityforreddit.customviews; + +import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.ViewParent; +import android.widget.HorizontalScrollView; + +import androidx.annotation.Nullable; +import androidx.viewpager2.widget.ViewPager2; + +import ml.docilealligator.infinityforreddit.customviews.slidr.widget.SliderPanel; + +public class TableHorizontalScrollView extends HorizontalScrollView { + @Nullable + private CustomToroContainer toroContainer; + @Nullable + private ViewPager2 viewPager2; + @Nullable + private SliderPanel sliderPanel; + + private float lastX = 0.0f; + private float lastY = 0.0f; + private boolean allowScroll; + private boolean isViewPager2Enabled; + + public TableHorizontalScrollView(Context context) { + super(context); + init(); + } + + public TableHorizontalScrollView(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + public TableHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(); + } + + public TableHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + init(); + } + + private void init() { + new Handler(Looper.getMainLooper()).post(() -> { + ViewParent parent = getParent(); + while (parent != null) { + if (parent instanceof CustomToroContainer) { + toroContainer = (CustomToroContainer) parent; + } else if (parent instanceof ViewPager2) { + viewPager2 = (ViewPager2) parent; + isViewPager2Enabled = viewPager2.isUserInputEnabled(); + } else if (parent instanceof SliderPanel) { + sliderPanel = (SliderPanel) parent; + } + + parent = parent.getParent(); + } + }); + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) { + processMotionEvent(ev); + return allowScroll; + } + + @Override + public boolean onTouchEvent(MotionEvent ev) { + processMotionEvent(ev); + return super.onTouchEvent(ev); + } + + private void processMotionEvent(MotionEvent ev) { + switch (ev.getActionMasked()) { + case MotionEvent.ACTION_DOWN: + lastX = ev.getX(); + lastY = ev.getY(); + allowScroll = true; + + if (toroContainer != null) { + toroContainer.requestDisallowInterceptTouchEvent(true); + } + if (viewPager2 != null && isViewPager2Enabled) { + viewPager2.setUserInputEnabled(false); + } + if (sliderPanel != null) { + sliderPanel.lock(); + } + break; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + allowScroll = false; + + if (toroContainer != null) { + toroContainer.requestDisallowInterceptTouchEvent(false); + } + if (viewPager2 != null && isViewPager2Enabled) { + viewPager2.setUserInputEnabled(true); + } + if (sliderPanel != null) { + sliderPanel.unlock(); + } + break; + case MotionEvent.ACTION_MOVE: + float currentX = ev.getX(); + float currentY = ev.getY(); + float dx = Math.abs(currentX - lastX); + float dy = Math.abs(currentY - lastY); + + allowScroll = dy < dx; + + if (toroContainer != null) { + toroContainer.requestDisallowInterceptTouchEvent(allowScroll); + } + if (viewPager2 != null && isViewPager2Enabled) { + viewPager2.setUserInputEnabled(false); + } + if (sliderPanel != null) { + sliderPanel.lock(); + } + break; + } + } +} diff --git a/app/src/main/res/layout/adapter_table_block.xml b/app/src/main/res/layout/adapter_table_block.xml index 29ce9279..35f6178b 100644 --- a/app/src/main/res/layout/adapter_table_block.xml +++ b/app/src/main/res/layout/adapter_table_block.xml @@ -1,5 +1,5 @@ - - + -- cgit v1.2.3