From 98b324f1902888f7382d4ed8d42eed4832ed4d5f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 3 Sep 2020 09:34:32 +0800 Subject: Implement barebone swipe to upvote or downvote. --- .../Adapter/PostRecyclerViewAdapter.java | 21 ++++++ .../infinityforreddit/Fragment/PostFragment.java | 77 ++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index c9cf8e96..13fa5d90 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -10,6 +10,7 @@ import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -30,6 +31,7 @@ import androidx.constraintlayout.widget.ConstraintSet; import androidx.core.graphics.drawable.DrawableCompat; import androidx.paging.PagedListAdapter; import androidx.recyclerview.widget.DiffUtil; +import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; @@ -509,10 +511,12 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 0) { + if (dX > (itemView.getRight() - itemView.getLeft()) * 0.3) { + backgroundLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); + } else { + backgroundLeft.setBounds(0, 0, 0, 0); + } + drawableLeft.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableLeft.getIntrinsicWidth(), (itemView.getBottom() - itemView.getTop() - drawableLeft.getIntrinsicHeight()) / 2, itemView.getLeft() + ((int) dX) - horizontalOffset, (itemView.getBottom() - itemView.getTop() + drawableLeft.getIntrinsicHeight()) / 2); + backgroundLeft.draw(c); + drawableLeft.draw(c); + } else if (dX < 0) { + if (-dX > (itemView.getRight() - itemView.getLeft()) * 0.3) { + backgroundRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); + } else { + backgroundRight.setBounds(0, 0, 0, 0); + } + drawableRight.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset, (itemView.getBottom() - itemView.getTop() - drawableRight.getIntrinsicHeight()) / 2, itemView.getRight() + ((int) dX) + horizontalOffset + drawableRight.getIntrinsicWidth(), (itemView.getBottom() - itemView.getTop() + drawableRight.getIntrinsicHeight()) / 2); + backgroundRight.draw(c); + drawableRight.draw(c); + } + } + + @Override + public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) { + return swipeActionThreshold; + } + }); + + touchHelper.attachToRecyclerView(mPostRecyclerView); mPostRecyclerView.setAdapter(mAdapter); mPostRecyclerView.setCacheManager(mAdapter); mPostRecyclerView.setPlayerInitializer(order -> { -- cgit v1.2.3