Fixed ItemTouchHelper conflict with Slidr.

This commit is contained in:
Docile-Alligator 2023-04-29 00:09:43 +08:00
parent d65a355bdc
commit ee86148209
4 changed files with 86 additions and 21 deletions

View File

@ -2227,6 +2227,19 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
} }
}); });
itemView.setOnTouchListener((v, event) -> {
if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) {
mActivity.unlockSwipeRightToGoBack();
}
} else {
if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) {
mActivity.lockSwipeRightToGoBack();
}
}
return false;
});
userTextView.setOnClickListener(view -> { userTextView.setOnClickListener(view -> {
if (canStartActivity) { if (canStartActivity) {
int position = getBindingAdapterPosition(); int position = getBindingAdapterPosition();
@ -3241,15 +3254,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
} }
}); });
rootView.setOnTouchListener((view, motionEvent) -> {
swipeLocked = false;
return false;
});
bottomConstraintLayout.setOnTouchListener((view, motionEvent) -> {
swipeLocked = false;
return false;
});
noPreviewImageView.setOnClickListener(view -> { noPreviewImageView.setOnClickListener(view -> {
int position = getBindingAdapterPosition(); int position = getBindingAdapterPosition();
if (position < 0) { if (position < 0) {
@ -3572,6 +3576,19 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
return true; return true;
}); });
itemView.setOnTouchListener((v, event) -> {
if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
if (mFragment.isRecyclerViewItemSwipeable(PostCompactBaseViewHolder.this)) {
mActivity.unlockSwipeRightToGoBack();
}
} else {
if (mFragment.isRecyclerViewItemSwipeable(PostCompactBaseViewHolder.this)) {
mActivity.lockSwipeRightToGoBack();
}
}
return false;
});
nameTextView.setOnClickListener(view -> { nameTextView.setOnClickListener(view -> {
int position = getBindingAdapterPosition(); int position = getBindingAdapterPosition();
if (position < 0) { if (position < 0) {

View File

@ -11,6 +11,7 @@ import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
@ -2332,6 +2333,19 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
} }
}); });
itemView.setOnTouchListener((v, event) -> {
if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) {
mActivity.unlockSwipeRightToGoBack();
}
} else {
if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) {
mActivity.lockSwipeRightToGoBack();
}
}
return false;
});
userTextView.setOnClickListener(view -> { userTextView.setOnClickListener(view -> {
if (!canStartActivity) { if (!canStartActivity) {
return; return;
@ -3379,15 +3393,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
} }
}); });
rootView.setOnTouchListener((view, motionEvent) -> {
swipeLocked = false;
return false;
});
bottomConstraintLayout.setOnTouchListener((view, motionEvent) -> {
swipeLocked = false;
return false;
});
noPreviewImageView.setOnClickListener(view -> { noPreviewImageView.setOnClickListener(view -> {
int position = getBindingAdapterPosition(); int position = getBindingAdapterPosition();
if (position < 0) { if (position < 0) {
@ -3714,6 +3719,19 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
return true; return true;
}); });
itemView.setOnTouchListener((v, event) -> {
if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
if (mFragment.isRecyclerViewItemSwipeable(PostCompactBaseViewHolder.this)) {
mActivity.unlockSwipeRightToGoBack();
}
} else {
if (mFragment.isRecyclerViewItemSwipeable(PostCompactBaseViewHolder.this)) {
mActivity.lockSwipeRightToGoBack();
}
}
return false;
});
nameTextView.setOnClickListener(view -> { nameTextView.setOnClickListener(view -> {
int position = getBindingAdapterPosition(); int position = getBindingAdapterPosition();
if (position < 0) { if (position < 0) {

View File

@ -209,6 +209,7 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato
private boolean isLazyModePaused = false; private boolean isLazyModePaused = false;
private boolean hasPost = false; private boolean hasPost = false;
private boolean rememberMutingOptionInPostFeed; private boolean rememberMutingOptionInPostFeed;
private boolean swipeActionEnabled;
private Boolean masterMutingOption; private Boolean masterMutingOption;
private HistoryPostRecyclerViewAdapter mAdapter; private HistoryPostRecyclerViewAdapter mAdapter;
private RecyclerView.SmoothScroller smoothScroller; private RecyclerView.SmoothScroller smoothScroller;
@ -563,6 +564,7 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato
}); });
if (nColumns == 1 && mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SWIPE_ACTION, false)) { if (nColumns == 1 && mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SWIPE_ACTION, false)) {
swipeActionEnabled = true;
touchHelper.attachToRecyclerView(mPostRecyclerView); touchHelper.attachToRecyclerView(mPostRecyclerView);
} }
mPostRecyclerView.setAdapter(mAdapter); mPostRecyclerView.setAdapter(mAdapter);
@ -1011,6 +1013,18 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato
} }
} }
public boolean isRecyclerViewItemSwipeable(RecyclerView.ViewHolder viewHolder) {
if (swipeActionEnabled) {
if (viewHolder instanceof HistoryPostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) {
return !((HistoryPostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) viewHolder).isSwipeLocked();
}
return true;
}
return false;
}
@Subscribe @Subscribe
public void onPostUpdateEvent(PostUpdateEventToPostList event) { public void onPostUpdateEvent(PostUpdateEventToPostList event) {
ItemSnapshotList<Post> posts = mAdapter.snapshot(); ItemSnapshotList<Post> posts = mAdapter.snapshot();
@ -1199,7 +1213,8 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato
@Subscribe @Subscribe
public void onChangeEnableSwipeActionSwitchEvent(ChangeEnableSwipeActionSwitchEvent changeEnableSwipeActionSwitchEvent) { public void onChangeEnableSwipeActionSwitchEvent(ChangeEnableSwipeActionSwitchEvent changeEnableSwipeActionSwitchEvent) {
if (touchHelper != null) { if (getNColumns(getResources()) == 1 && touchHelper != null) {
swipeActionEnabled = changeEnableSwipeActionSwitchEvent.enableSwipeAction;
if (changeEnableSwipeActionSwitchEvent.enableSwipeAction) { if (changeEnableSwipeActionSwitchEvent.enableSwipeAction) {
touchHelper.attachToRecyclerView(mPostRecyclerView); touchHelper.attachToRecyclerView(mPostRecyclerView);
} else { } else {

View File

@ -16,6 +16,7 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.CountDownTimer; import android.os.CountDownTimer;
import android.os.Handler; import android.os.Handler;
import android.util.Log;
import android.view.HapticFeedbackConstants; import android.view.HapticFeedbackConstants;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -233,6 +234,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private boolean hasPost = false; private boolean hasPost = false;
private boolean savePostFeedScrolledPosition; private boolean savePostFeedScrolledPosition;
private boolean rememberMutingOptionInPostFeed; private boolean rememberMutingOptionInPostFeed;
private boolean swipeActionEnabled;
private Boolean masterMutingOption; private Boolean masterMutingOption;
private PostRecyclerViewAdapter mAdapter; private PostRecyclerViewAdapter mAdapter;
private RecyclerView.SmoothScroller smoothScroller; private RecyclerView.SmoothScroller smoothScroller;
@ -1064,7 +1066,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if (((PostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) viewHolder).isSwipeLocked()) { if (((PostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) viewHolder).isSwipeLocked()) {
return makeMovementFlags(0, 0); return makeMovementFlags(0, 0);
} }
} }
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(0, swipeFlags); return makeMovementFlags(0, swipeFlags);
@ -1153,6 +1154,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
}); });
if (nColumns == 1 && mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SWIPE_ACTION, false)) { if (nColumns == 1 && mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SWIPE_ACTION, false)) {
swipeActionEnabled = true;
touchHelper.attachToRecyclerView(mPostRecyclerView); touchHelper.attachToRecyclerView(mPostRecyclerView);
} }
mPostRecyclerView.setAdapter(mAdapter); mPostRecyclerView.setAdapter(mAdapter);
@ -1978,7 +1980,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Subscribe @Subscribe
public void onChangeEnableSwipeActionSwitchEvent(ChangeEnableSwipeActionSwitchEvent changeEnableSwipeActionSwitchEvent) { public void onChangeEnableSwipeActionSwitchEvent(ChangeEnableSwipeActionSwitchEvent changeEnableSwipeActionSwitchEvent) {
if (touchHelper != null) { if (getNColumns(getResources()) == 1 && touchHelper != null) {
swipeActionEnabled = changeEnableSwipeActionSwitchEvent.enableSwipeAction;
if (changeEnableSwipeActionSwitchEvent.enableSwipeAction) { if (changeEnableSwipeActionSwitchEvent.enableSwipeAction) {
touchHelper.attachToRecyclerView(mPostRecyclerView); touchHelper.attachToRecyclerView(mPostRecyclerView);
} else { } else {
@ -2219,6 +2222,18 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} }
} }
public boolean isRecyclerViewItemSwipeable(RecyclerView.ViewHolder viewHolder) {
if (swipeActionEnabled) {
if (viewHolder instanceof PostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) {
return !((PostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) viewHolder).isSwipeLocked();
}
return true;
}
return false;
}
private static abstract class LazyModeRunnable implements Runnable { private static abstract class LazyModeRunnable implements Runnable {
private int currentPosition = -1; private int currentPosition = -1;