diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-10-29 02:56:13 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-10-29 02:56:13 +0000 |
commit | 14bcbd5f58494efe044543e943976faea523d650 (patch) | |
tree | cf25cf5ff36f5053e1a4514caab4e93efaf24fa8 | |
parent | a1ba02402e5bb8a3b8c20072541f49af48dd3385 (diff) | |
download | infinity-for-reddit-14bcbd5f58494efe044543e943976faea523d650.tar infinity-for-reddit-14bcbd5f58494efe044543e943976faea523d650.tar.gz infinity-for-reddit-14bcbd5f58494efe044543e943976faea523d650.tar.bz2 infinity-for-reddit-14bcbd5f58494efe044543e943976faea523d650.tar.lz infinity-for-reddit-14bcbd5f58494efe044543e943976faea523d650.tar.xz infinity-for-reddit-14bcbd5f58494efe044543e943976faea523d650.tar.zst infinity-for-reddit-14bcbd5f58494efe044543e943976faea523d650.zip |
Refine ItemTouchHelper onChildDraw in ViewPostDetailFragment.
2 files changed, 176 insertions, 195 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/slidr/widget/SliderPanel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/slidr/widget/SliderPanel.java index 07be1546..046a430a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/slidr/widget/SliderPanel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/slidr/widget/SliderPanel.java @@ -35,16 +35,14 @@ public class SliderPanel extends FrameLayout { private SlidrConfig config; + public SliderPanel(Context context) { + super(context); + } - public SliderPanel(Context context) { - super(context); - } - - - public SliderPanel(Context context, View decorView, SlidrConfig config){ + public SliderPanel(Context context, View decorView, SlidrConfig config) { super(context); this.decorView = decorView; - this.config = (config == null ? new SlidrConfig.Builder().build() : config); + this.config = (config == null ? new SlidrConfig.Builder().build() : config); init(); } @@ -57,11 +55,11 @@ public class SliderPanel extends FrameLayout { public boolean onInterceptTouchEvent(MotionEvent ev) { boolean interceptForDrag; - if(isLocked){ + if (isLocked) { return false; } - if(config.isEdgeOnly()) { + if (config.isEdgeOnly()) { isEdgeTouched = canDragFromEdge(ev); } @@ -75,16 +73,15 @@ public class SliderPanel extends FrameLayout { return interceptForDrag && !isLocked; } - @Override public boolean onTouchEvent(MotionEvent event) { - if(isLocked){ + if (isLocked) { return false; } try { dragHelper.processTouchEvent(event); - }catch (IllegalArgumentException e){ + } catch (IllegalArgumentException e) { return false; } @@ -94,27 +91,25 @@ public class SliderPanel extends FrameLayout { @Override public void computeScroll() { super.computeScroll(); - if(dragHelper.continueSettling(true)){ + if (dragHelper.continueSettling(true)) { ViewCompat.postInvalidateOnAnimation(this); } } - @Override protected void onDraw(Canvas canvas) { scrimRenderer.render(canvas, config.getPosition(), scrimPaint); } - /** * Set the panel slide listener that gets called based on slider changes + * * @param listener callback implementation */ - public void setOnPanelSlideListener(OnPanelSlideListener listener){ + public void setOnPanelSlideListener(OnPanelSlideListener listener) { this.listener = listener; } - /** * Get the default {@link SlidrInterface} from which to control the panel with after attachment */ @@ -126,10 +121,7 @@ public class SliderPanel extends FrameLayout { return isLocked; } - private final SlidrInterface defaultSlidrInterface = new SlidrInterface() { - - @Override public void lock() { SliderPanel.this.lock(); @@ -142,7 +134,6 @@ public class SliderPanel extends FrameLayout { } }; - /** * The drag helper callback interface for the Left position */ @@ -173,16 +164,16 @@ public class SliderPanel extends FrameLayout { int leftThreshold = (int) (getWidth() * config.getDistanceThreshold()); boolean isVerticalSwiping = Math.abs(yvel) > config.getVelocityThreshold(); - if(xvel > 0){ + if (xvel > 0) { - if(Math.abs(xvel) > config.getVelocityThreshold() && !isVerticalSwiping){ + if (Math.abs(xvel) > config.getVelocityThreshold() && !isVerticalSwiping) { settleLeft = screenWidth; - }else if(left > leftThreshold){ + } else if (left > leftThreshold) { settleLeft = screenWidth; } - }else if(xvel == 0){ - if(left > leftThreshold){ + } else if (xvel == 0) { + if (left > leftThreshold) { settleLeft = screenWidth; } } @@ -194,9 +185,9 @@ public class SliderPanel extends FrameLayout { @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); - float percent = 1f - ((float)left / (float) screenWidth); + float percent = 1f - ((float) left / (float) screenWidth); - if(listener != null) listener.onSlideChange(percent); + if (listener != null) listener.onSlideChange(percent); // Update the dimmer alpha applyScrim(percent); @@ -205,15 +196,15 @@ public class SliderPanel extends FrameLayout { @Override public void onViewDragStateChanged(int state) { super.onViewDragStateChanged(state); - if(listener != null) listener.onStateChanged(state); - switch (state){ + if (listener != null) listener.onStateChanged(state); + switch (state) { case ViewDragHelper.STATE_IDLE: - if(decorView.getLeft() == 0){ + if (decorView.getLeft() == 0) { // State Open - if(listener != null) listener.onOpened(); - }else{ + if (listener != null) listener.onOpened(); + } else { // State Closed - if(listener != null) listener.onClosed(); + if (listener != null) listener.onClosed(); } break; case ViewDragHelper.STATE_DRAGGING: @@ -227,7 +218,6 @@ public class SliderPanel extends FrameLayout { }; - /** * The drag helper callbacks for dragging the slidr attachment from the right of the screen */ @@ -257,16 +247,16 @@ public class SliderPanel extends FrameLayout { int leftThreshold = (int) (getWidth() * config.getDistanceThreshold()); boolean isVerticalSwiping = Math.abs(yvel) > config.getVelocityThreshold(); - if(xvel < 0){ + if (xvel < 0) { - if(Math.abs(xvel) > config.getVelocityThreshold() && !isVerticalSwiping){ + if (Math.abs(xvel) > config.getVelocityThreshold() && !isVerticalSwiping) { settleLeft = -screenWidth; - }else if(left < -leftThreshold){ + } else if (left < -leftThreshold) { settleLeft = -screenWidth; } - }else if(xvel == 0){ - if(left < -leftThreshold){ + } else if (xvel == 0) { + if (left < -leftThreshold) { settleLeft = -screenWidth; } } @@ -278,9 +268,9 @@ public class SliderPanel extends FrameLayout { @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); - float percent = 1f - ((float)Math.abs(left) / (float) screenWidth); + float percent = 1f - ((float) Math.abs(left) / (float) screenWidth); - if(listener != null) listener.onSlideChange(percent); + if (listener != null) listener.onSlideChange(percent); // Update the dimmer alpha applyScrim(percent); @@ -289,15 +279,15 @@ public class SliderPanel extends FrameLayout { @Override public void onViewDragStateChanged(int state) { super.onViewDragStateChanged(state); - if(listener != null) listener.onStateChanged(state); - switch (state){ + if (listener != null) listener.onStateChanged(state); + switch (state) { case ViewDragHelper.STATE_IDLE: - if(decorView.getLeft() == 0){ + if (decorView.getLeft() == 0) { // State Open - if(listener != null) listener.onOpened(); - }else{ + if (listener != null) listener.onOpened(); + } else { // State Closed - if(listener != null) listener.onClosed(); + if (listener != null) listener.onClosed(); } break; case ViewDragHelper.STATE_DRAGGING: @@ -310,7 +300,6 @@ public class SliderPanel extends FrameLayout { } }; - /** * The drag helper callbacks for dragging the slidr attachment from the top of the screen */ @@ -339,14 +328,14 @@ public class SliderPanel extends FrameLayout { int topThreshold = (int) (getHeight() * config.getDistanceThreshold()); boolean isSideSwiping = Math.abs(xvel) > config.getVelocityThreshold(); - if(yvel > 0){ - if(Math.abs(yvel) > config.getVelocityThreshold() && !isSideSwiping){ + if (yvel > 0) { + if (Math.abs(yvel) > config.getVelocityThreshold() && !isSideSwiping) { settleTop = screenHeight; - }else if(top > topThreshold){ + } else if (top > topThreshold) { settleTop = screenHeight; } - }else if(yvel == 0){ - if(top > topThreshold){ + } else if (yvel == 0) { + if (top > topThreshold) { settleTop = screenHeight; } } @@ -358,9 +347,9 @@ public class SliderPanel extends FrameLayout { @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); - float percent = 1f - ((float)Math.abs(top) / (float) screenHeight); + float percent = 1f - ((float) Math.abs(top) / (float) screenHeight); - if(listener != null) listener.onSlideChange(percent); + if (listener != null) listener.onSlideChange(percent); // Update the dimmer alpha applyScrim(percent); @@ -369,15 +358,15 @@ public class SliderPanel extends FrameLayout { @Override public void onViewDragStateChanged(int state) { super.onViewDragStateChanged(state); - if(listener != null) listener.onStateChanged(state); - switch (state){ + if (listener != null) listener.onStateChanged(state); + switch (state) { case ViewDragHelper.STATE_IDLE: - if(decorView.getTop() == 0){ + if (decorView.getTop() == 0) { // State Open - if(listener != null) listener.onOpened(); - }else{ + if (listener != null) listener.onOpened(); + } else { // State Closed - if(listener != null) listener.onClosed(); + if (listener != null) listener.onClosed(); } break; case ViewDragHelper.STATE_DRAGGING: @@ -390,7 +379,6 @@ public class SliderPanel extends FrameLayout { } }; - /** * The drag helper callbacks for dragging the slidr attachment from the bottom of the screen */ @@ -419,14 +407,14 @@ public class SliderPanel extends FrameLayout { int topThreshold = (int) (getHeight() * config.getDistanceThreshold()); boolean isSideSwiping = Math.abs(xvel) > config.getVelocityThreshold(); - if(yvel < 0){ - if(Math.abs(yvel) > config.getVelocityThreshold() && !isSideSwiping){ + if (yvel < 0) { + if (Math.abs(yvel) > config.getVelocityThreshold() && !isSideSwiping) { settleTop = -screenHeight; - }else if(top < -topThreshold){ + } else if (top < -topThreshold) { settleTop = -screenHeight; } - }else if(yvel == 0){ - if(top < -topThreshold){ + } else if (yvel == 0) { + if (top < -topThreshold) { settleTop = -screenHeight; } } @@ -438,9 +426,9 @@ public class SliderPanel extends FrameLayout { @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); - float percent = 1f - ((float)Math.abs(top) / (float) screenHeight); + float percent = 1f - ((float) Math.abs(top) / (float) screenHeight); - if(listener != null) listener.onSlideChange(percent); + if (listener != null) listener.onSlideChange(percent); // Update the dimmer alpha applyScrim(percent); @@ -449,15 +437,15 @@ public class SliderPanel extends FrameLayout { @Override public void onViewDragStateChanged(int state) { super.onViewDragStateChanged(state); - if(listener != null) listener.onStateChanged(state); - switch (state){ + if (listener != null) listener.onStateChanged(state); + switch (state) { case ViewDragHelper.STATE_IDLE: - if(decorView.getTop() == 0){ + if (decorView.getTop() == 0) { // State Open - if(listener != null) listener.onOpened(); - }else{ + if (listener != null) listener.onOpened(); + } else { // State Closed - if(listener != null) listener.onClosed(); + if (listener != null) listener.onClosed(); } break; case ViewDragHelper.STATE_DRAGGING: @@ -470,7 +458,6 @@ public class SliderPanel extends FrameLayout { } }; - /** * The drag helper callbacks for dragging the slidr attachment in both vertical directions */ @@ -499,28 +486,28 @@ public class SliderPanel extends FrameLayout { int topThreshold = (int) (getHeight() * config.getDistanceThreshold()); boolean isSideSwiping = Math.abs(xvel) > config.getVelocityThreshold(); - if(yvel > 0){ + if (yvel > 0) { // Being slinged down - if(Math.abs(yvel) > config.getVelocityThreshold() && !isSideSwiping){ + if (Math.abs(yvel) > config.getVelocityThreshold() && !isSideSwiping) { settleTop = screenHeight; - }else if(top > topThreshold){ + } else if (top > topThreshold) { settleTop = screenHeight; } - }else if(yvel < 0){ + } else if (yvel < 0) { // Being slinged up - if(Math.abs(yvel) > config.getVelocityThreshold() && !isSideSwiping){ + if (Math.abs(yvel) > config.getVelocityThreshold() && !isSideSwiping) { settleTop = -screenHeight; - }else if(top < -topThreshold){ + } else if (top < -topThreshold) { settleTop = -screenHeight; } - }else{ + } else { - if(top > topThreshold){ + if (top > topThreshold) { settleTop = screenHeight; - }else if(top < -topThreshold){ + } else if (top < -topThreshold) { settleTop = -screenHeight; } @@ -533,9 +520,9 @@ public class SliderPanel extends FrameLayout { @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); - float percent = 1f - ((float)Math.abs(top) / (float) screenHeight); + float percent = 1f - ((float) Math.abs(top) / (float) screenHeight); - if(listener != null) listener.onSlideChange(percent); + if (listener != null) listener.onSlideChange(percent); // Update the dimmer alpha applyScrim(percent); @@ -544,15 +531,15 @@ public class SliderPanel extends FrameLayout { @Override public void onViewDragStateChanged(int state) { super.onViewDragStateChanged(state); - if(listener != null) listener.onStateChanged(state); - switch (state){ + if (listener != null) listener.onStateChanged(state); + switch (state) { case ViewDragHelper.STATE_IDLE: - if(decorView.getTop() == 0){ + if (decorView.getTop() == 0) { // State Open - if(listener != null) listener.onOpened(); - }else{ + if (listener != null) listener.onOpened(); + } else { // State Closed - if(listener != null) listener.onClosed(); + if (listener != null) listener.onClosed(); } break; case ViewDragHelper.STATE_DRAGGING: @@ -565,7 +552,6 @@ public class SliderPanel extends FrameLayout { } }; - /** * The drag helper callbacks for dragging the slidr attachment in both horizontal directions */ @@ -595,26 +581,26 @@ public class SliderPanel extends FrameLayout { int leftThreshold = (int) (getWidth() * config.getDistanceThreshold()); boolean isVerticalSwiping = Math.abs(yvel) > config.getVelocityThreshold(); - if(xvel > 0){ + if (xvel > 0) { - if(Math.abs(xvel) > config.getVelocityThreshold() && !isVerticalSwiping){ + if (Math.abs(xvel) > config.getVelocityThreshold() && !isVerticalSwiping) { settleLeft = screenWidth; - }else if(left > leftThreshold){ + } else if (left > leftThreshold) { settleLeft = screenWidth; } - }else if(xvel < 0){ + } else if (xvel < 0) { - if(Math.abs(xvel) > config.getVelocityThreshold() && !isVerticalSwiping){ + if (Math.abs(xvel) > config.getVelocityThreshold() && !isVerticalSwiping) { settleLeft = -screenWidth; - }else if(left < -leftThreshold){ + } else if (left < -leftThreshold) { settleLeft = -screenWidth; } - }else{ - if(left > leftThreshold){ + } else { + if (left > leftThreshold) { settleLeft = screenWidth; - }else if(left < -leftThreshold){ + } else if (left < -leftThreshold) { settleLeft = -screenWidth; } } @@ -626,9 +612,9 @@ public class SliderPanel extends FrameLayout { @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); - float percent = 1f - ((float)Math.abs(left) / (float) screenWidth); + float percent = 1f - ((float) Math.abs(left) / (float) screenWidth); - if(listener != null) listener.onSlideChange(percent); + if (listener != null) listener.onSlideChange(percent); // Update the dimmer alpha applyScrim(percent); @@ -637,15 +623,15 @@ public class SliderPanel extends FrameLayout { @Override public void onViewDragStateChanged(int state) { super.onViewDragStateChanged(state); - if(listener != null) listener.onStateChanged(state); - switch (state){ + if (listener != null) listener.onStateChanged(state); + switch (state) { case ViewDragHelper.STATE_IDLE: - if(decorView.getLeft() == 0){ + if (decorView.getLeft() == 0) { // State Open - if(listener != null) listener.onOpened(); - }else{ + if (listener != null) listener.onOpened(); + } else { // State Closed - if(listener != null) listener.onClosed(); + if (listener != null) listener.onClosed(); } break; case ViewDragHelper.STATE_DRAGGING: @@ -658,8 +644,7 @@ public class SliderPanel extends FrameLayout { } }; - - private void init(){ + private void init() { setWillNotDraw(false); screenWidth = getResources().getDisplayMetrics().widthPixels; @@ -667,12 +652,12 @@ public class SliderPanel extends FrameLayout { final float minVel = MIN_FLING_VELOCITY * density; ViewDragHelper.Callback callback; - switch (config.getPosition()){ + switch (config.getPosition()) { case LEFT: callback = leftCallback; edgePosition = ViewDragHelper.EDGE_LEFT; break; - case RIGHT: + case RIGHT: callback = rightCallback; edgePosition = ViewDragHelper.EDGE_RIGHT; break; @@ -714,28 +699,19 @@ public class SliderPanel extends FrameLayout { * ignore the system navigation that would be included if we * retrieved this value from the DisplayMetrics */ - post(new Runnable() { - @Override - public void run() { - screenHeight = getHeight(); - } - }); - + post(() -> screenHeight = getHeight()); } - - public void lock(){ + public void lock() { dragHelper.abort(); isLocked = true; } - - public void unlock(){ + public void unlock() { dragHelper.abort(); isLocked = false; } - private boolean canDragFromEdge(MotionEvent ev) { float x = ev.getX(); float y = ev.getY(); @@ -757,33 +733,31 @@ public class SliderPanel extends FrameLayout { return false; } - - private void applyScrim(float percent){ + private void applyScrim(float percent) { float alpha = (percent * (config.getScrimStartAlpha() - config.getScrimEndAlpha())) + config.getScrimEndAlpha(); scrimPaint.setAlpha(toAlpha(alpha)); invalidate(scrimRenderer.getDirtyRect(config.getPosition())); } - - private static int clamp(int value, int min, int max){ + private static int clamp(int value, int min, int max) { return Math.max(min, Math.min(max, value)); } - private static int toAlpha(float percentage) { return (int) (percentage * 255); } - /** * The panel sliding interface that gets called * whenever the panel is closed or opened */ - public interface OnPanelSlideListener{ + public interface OnPanelSlideListener { void onStateChanged(int state); + void onClosed(); + void onOpened(); + void onSlideChange(float percent); } - } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index 66535d27..28b0a377 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -23,6 +23,7 @@ import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; @@ -48,7 +49,6 @@ import com.evernote.android.state.State; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.livefront.bridge.Bridge; -import ml.docilealligator.infinityforreddit.readpost.ReadPostsUtils; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -62,14 +62,9 @@ import javax.inject.Inject; import javax.inject.Named; import javax.inject.Provider; -import ml.docilealligator.infinityforreddit.thing.DeleteThing; -import ml.docilealligator.infinityforreddit.subreddit.Flair; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.thing.SaveThing; -import ml.docilealligator.infinityforreddit.thing.ReplyNotificationsToggle; -import ml.docilealligator.infinityforreddit.thing.SortType; import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.activities.CommentActivity; import ml.docilealligator.infinityforreddit.activities.EditPostActivity; @@ -104,8 +99,14 @@ import ml.docilealligator.infinityforreddit.post.HidePost; import ml.docilealligator.infinityforreddit.post.ParsePost; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; +import ml.docilealligator.infinityforreddit.readpost.ReadPostsUtils; import ml.docilealligator.infinityforreddit.subreddit.FetchSubredditData; +import ml.docilealligator.infinityforreddit.subreddit.Flair; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.thing.DeleteThing; +import ml.docilealligator.infinityforreddit.thing.ReplyNotificationsToggle; +import ml.docilealligator.infinityforreddit.thing.SaveThing; +import ml.docilealligator.infinityforreddit.thing.SortType; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -229,6 +230,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic private int swipeRightAction; private float swipeActionThreshold; private ItemTouchHelper touchHelper; + private boolean shouldSwipeBack; private int scrollPosition; private FragmentViewPostDetailBinding binding; private RecyclerView mCommentsRecyclerView; @@ -432,79 +434,84 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } @Override - public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { - if (touchHelper != null) { - exceedThreshold = false; - touchHelper.attachToRecyclerView(null); - touchHelper.attachToRecyclerView((mCommentsRecyclerView == null ? binding.postDetailRecyclerViewViewPostDetailFragment : mCommentsRecyclerView)); + public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {} + + @Override + public int convertToAbsoluteDirection(int flags, int layoutDirection) { + if (shouldSwipeBack) { + shouldSwipeBack = false; + return 0; } + return super.convertToAbsoluteDirection(flags, layoutDirection); } @Override public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { - super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); - - if (isCurrentlyActive) { - View itemView = viewHolder.itemView; - int horizontalOffset = (int) Utils.convertDpToPixel(16, activity); - if (dX > 0) { - if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { - if (!exceedThreshold) { - exceedThreshold = true; - if (vibrateWhenActionTriggered) { - viewHolder.itemView.setHapticFeedbackEnabled(true); - viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); - } + View itemView = viewHolder.itemView; + int horizontalOffset = (int) Utils.convertDpToPixel(16, activity); + if (dX > 0) { + if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { + dX = (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold; + if (!exceedThreshold && isCurrentlyActive) { + exceedThreshold = true; + if (vibrateWhenActionTriggered) { + itemView.setHapticFeedbackEnabled(true); + itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); } - backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); - } else { - exceedThreshold = false; - backgroundSwipeRight.setBounds(0, 0, 0, 0); } + backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); + } else { + exceedThreshold = false; + backgroundSwipeRight.setBounds(0, 0, 0, 0); + } - drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(), - (itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2, - itemView.getLeft() + ((int) dX) - horizontalOffset, - (itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2); - backgroundSwipeRight.draw(c); - drawableSwipeRight.draw(c); - } else if (dX < 0) { - if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { - if (!exceedThreshold) { - exceedThreshold = true; - if (vibrateWhenActionTriggered) { - viewHolder.itemView.setHapticFeedbackEnabled(true); - viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); - } + drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(), + (itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2, + itemView.getLeft() + ((int) dX) - horizontalOffset, + (itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2); + backgroundSwipeRight.draw(c); + drawableSwipeRight.draw(c); + } else if (dX < 0) { + if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { + dX = -(itemView.getRight() - itemView.getLeft()) * swipeActionThreshold; + if (!exceedThreshold && isCurrentlyActive) { + exceedThreshold = true; + if (vibrateWhenActionTriggered) { + itemView.setHapticFeedbackEnabled(true); + itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); } - backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); - } else { - exceedThreshold = false; - backgroundSwipeLeft.setBounds(0, 0, 0, 0); - } - drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset, - (itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2, - itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(), - (itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2); - backgroundSwipeLeft.draw(c); - drawableSwipeLeft.draw(c); - } - } else { - if (exceedThreshold) { - if (mCommentsAdapter != null) { - mCommentsAdapter.onItemSwipe(viewHolder, dX > 0 ? ItemTouchHelper.END : ItemTouchHelper.START, swipeLeftAction, swipeRightAction); } + backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); + } else { exceedThreshold = false; + backgroundSwipeLeft.setBounds(0, 0, 0, 0); } + drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset, + (itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2, + itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(), + (itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2); + backgroundSwipeLeft.draw(c); + drawableSwipeLeft.draw(c); + } + + if (!isCurrentlyActive && exceedThreshold && mCommentsAdapter != null) { + mCommentsAdapter.onItemSwipe(viewHolder, dX > 0 ? ItemTouchHelper.END : ItemTouchHelper.START, swipeLeftAction, swipeRightAction); + exceedThreshold = false; } + super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } @Override public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) { - return 100; + return 1; } }); + (mCommentsRecyclerView == null ? binding.postDetailRecyclerViewViewPostDetailFragment : mCommentsRecyclerView).setOnTouchListener((view, motionEvent) -> { + shouldSwipeBack = motionEvent.getAction() == MotionEvent.ACTION_CANCEL || motionEvent.getAction() == MotionEvent.ACTION_UP; + return false; + }); + if (enableSwipeAction) { touchHelper.attachToRecyclerView((mCommentsRecyclerView == null ? binding.postDetailRecyclerViewViewPostDetailFragment : mCommentsRecyclerView)); } |