Fix ItemTouchHelper and gallery swipe gesture fighting each other.

This commit is contained in:
Docile-Alligator 2022-11-13 16:50:25 +11:00
parent b85ed3c867
commit 6f2eea90ae
6 changed files with 66 additions and 25 deletions

View File

@ -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) {

View File

@ -400,4 +400,8 @@ public abstract class BaseActivity extends AppCompatActivity implements CustomFo
public void unlockSwipeRightToGoBack() {
}
public void toggleViewPagerSwipeLock(boolean lock) {
}
}

View File

@ -37,19 +37,16 @@ public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapte
private ArrayList<Post.Gallery> 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);
}
}

View File

@ -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<Post, RecyclerVie
binding.imageIndexTextViewItemPostGalleryType.setBorderColor(mMediaIndicatorBackgroundColor);
adapter = new PostGalleryTypeImageRecyclerViewAdapter(mGlide, mActivity.typeface,
mSaveMemoryCenterInsideDownsampleStrategy, mColorAccent, mPrimaryTextColor, mScale,
galleryItemIndex -> {
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<Post, RecyclerVie
@Override
public void setSwipeLocked(boolean swipeLocked) {
PostGalleryTypeViewHolder.this.swipeLocked = swipeLocked;
mActivity.toggleViewPagerSwipeLock(swipeLocked);
}
});
binding.galleryRecyclerViewItemPostGalleryType.setLayoutManager(layoutManager);
@ -3184,6 +3177,55 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
binding.imageIndexTextViewItemPostGalleryType.setText(mActivity.getString(R.string.image_index_in_gallery, layoutManager.findFirstVisibleItemPosition() + 1, post.getGallery().size()));
}
});
binding.galleryRecyclerViewItemPostGalleryType.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
private float downX;
private float downY;
private boolean dragged;
private final int minTouchSlop = ViewConfiguration.get(mActivity).getScaledTouchSlop();
@Override
public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downX = e.getRawX();
downY = e.getRawY();
break;
case MotionEvent.ACTION_MOVE:
if(Math.abs(e.getRawX() - downX) > 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;

View File

@ -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

View File

@ -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;