From 6f2eea90ae8ad00d57e6a04f0f98c1dbd5c01b9b Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sun, 13 Nov 2022 16:50:25 +1100 Subject: [PATCH] Fix ItemTouchHelper and gallery swipe gesture fighting each other. --- .../activities/AccountSavedThingActivity.java | 5 ++ .../activities/BaseActivity.java | 4 ++ ...stGalleryTypeImageRecyclerViewAdapter.java | 13 +--- .../adapters/PostRecyclerViewAdapter.java | 66 +++++++++++++++---- .../customviews/SwipeLockFrameLayout.java | 2 +- .../fragments/PostFragment.java | 1 + 6 files changed, 66 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java index 90364417..b9ffb7fe 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java @@ -243,6 +243,11 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT } } + @Override + public void toggleViewPagerSwipeLock(boolean lock) { + binding.accountSavedThingViewPager2.setUserInputEnabled(!lock); + } + @Override public void postLayoutSelected(int postLayout) { if (sectionsPagerAdapter != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java index 02c20b77..de0b8947 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java @@ -400,4 +400,8 @@ public abstract class BaseActivity extends AppCompatActivity implements CustomFo public void unlockSwipeRightToGoBack() { } + + public void toggleViewPagerSwipeLock(boolean lock) { + + } } \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java index cbf34984..c9d61bf8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java @@ -37,19 +37,16 @@ public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapte private ArrayList galleryImages; private boolean blurImage; private float ratio; - private OnItemClickListener onItemClickListener; public PostGalleryTypeImageRecyclerViewAdapter(RequestManager glide, Typeface typeface, SaveMemoryCenterInisdeDownsampleStrategy saveMemoryCenterInisdeDownsampleStrategy, - int mColorAccent, int mPrimaryTextColor, float scale, - OnItemClickListener onItemClickListener) { + int mColorAccent, int mPrimaryTextColor, float scale) { this.glide = glide; this.typeface = typeface; this.saveMemoryCenterInisdeDownsampleStrategy = saveMemoryCenterInisdeDownsampleStrategy; this.mColorAccent = mColorAccent; this.mPrimaryTextColor = mPrimaryTextColor; this.mScale = scale; - this.onItemClickListener = onItemClickListener; } @NonNull @@ -137,10 +134,6 @@ public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapte binding.progressBarItemGalleryImageInPostFeed.setIndeterminateTintList(ColorStateList.valueOf(mColorAccent)); binding.errorTextViewItemGalleryImageInPostFeed.setTextColor(mPrimaryTextColor); - binding.imageViewItemGalleryImageInPostFeed.setOnClickListener(view -> { - onItemClickListener.onClick(getBindingAdapterPosition()); - }); - binding.errorTextViewItemGalleryImageInPostFeed.setOnClickListener(view -> { binding.progressBarItemGalleryImageInPostFeed.setVisibility(View.VISIBLE); binding.errorTextViewItemGalleryImageInPostFeed.setVisibility(View.GONE); @@ -148,8 +141,4 @@ public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapte }); } } - - public interface OnItemClickListener { - void onClick(int galleryItemIndex); - } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 81c4c441..6c52426b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -11,8 +11,11 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Handler; +import android.util.Log; import android.view.LayoutInflater; +import android.view.MotionEvent; import android.view.View; +import android.view.ViewConfiguration; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.ImageView; @@ -32,7 +35,6 @@ import androidx.core.content.ContextCompat; import androidx.paging.PagingDataAdapter; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.ItemTouchHelper; -import androidx.recyclerview.widget.LinearSnapHelper; import androidx.recyclerview.widget.PagerSnapHelper; import androidx.recyclerview.widget.RecyclerView; @@ -3138,17 +3140,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - if (post != null) { - markPostRead(post, true); - openMedia(post, galleryItemIndex); - } - }); + mSaveMemoryCenterInsideDownsampleStrategy, mColorAccent, mPrimaryTextColor, mScale); binding.galleryRecyclerViewItemPostGalleryType.setAdapter(adapter); new PagerSnapHelper().attachToRecyclerView(binding.galleryRecyclerViewItemPostGalleryType); binding.galleryRecyclerViewItemPostGalleryType.setRecycledViewPool(mGalleryRecycledViewPool); @@ -3169,6 +3161,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter minTouchSlop || Math.abs(e.getRawY() - downY) > minTouchSlop) { + dragged = true; + } + break; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + if (!dragged) { + int index = layoutManager.findFirstVisibleItemPosition(); + int position = getBindingAdapterPosition(); + if (position >= 0) { + if (post != null) { + markPostRead(post, true); + openMedia(post, index); + } + } + } + + downX = 0; + downY = 0; + dragged = false; + } + return false; + } + + @Override + public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) { + + } + + @Override + public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { + + } + }); binding.getRoot().setOnTouchListener((view, motionEvent) -> { swipeLocked = false; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/SwipeLockFrameLayout.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/SwipeLockFrameLayout.java index 72d3e9b7..5b62bbfb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/SwipeLockFrameLayout.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/SwipeLockFrameLayout.java @@ -38,7 +38,7 @@ public class SwipeLockFrameLayout extends FrameLayout implements SwipeLockView { @Override public boolean onInterceptTouchEvent(MotionEvent ev) { updateSwipeLock(ev); - return false; + return locked; } @SuppressLint("ClickableViewAccessibility") // we are just listening to touch events diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index 32c98b04..e0423ed0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -16,6 +16,7 @@ import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; +import android.util.Log; import android.view.HapticFeedbackConstants; import android.view.KeyEvent; import android.view.LayoutInflater;