使用 ItemTouchHelper 向右滑动以显示 RecyclerView 上的按钮

问题描述 投票:0回答:2

我正在尝试在 RecyclerView 右滑动中显示按钮。我使用 this 答案

成功在 RecyclerView 上向左滑动显示了这些按钮

enter image description here

但我无法在另一侧显示这些按钮。我尝试了this中的许多其他答案来实现此功能,但没有取得任何成功。请任何人帮助我完成这项任务。

提前谢谢

java android kotlin android-recyclerview
2个回答
3
投票

我正在努力解决这个问题,但现在我解决了,这就是解决方案

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public abstract class SwipeHelperRight extends ItemTouchHelper.SimpleCallback {


public static final int BUTTON_WIDTH = 140;
private RecyclerView recyclerView;
private List<SwipeHelperRight.UnderlayButton> buttons;
private GestureDetector gestureDetector;
private int swipedPos = -1;
private float swipeThreshold = 0.5f;
private Map<Integer, List<SwipeHelperRight.UnderlayButton>> buttonsBuffer;
private Queue<Integer> recoverQueue;

private GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener(){
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        for (SwipeHelperRight.UnderlayButton button : buttons){
            if(button.onClick(e.getX(), e.getY()))
                break;
        }

        return true;
    }
};

public SwipeHelperRight(Context context, RecyclerView recyclerView) {
    super(0, ItemTouchHelper.RIGHT);
    this.recyclerView = recyclerView;
    this.buttons = new ArrayList<>();
    this.gestureDetector = new GestureDetector(context, gestureListener);
    this.recyclerView.setOnTouchListener(onTouchListener);
    buttonsBuffer = new HashMap<>();
    recoverQueue = new LinkedList<Integer>(){
        @Override
        public boolean add(Integer o) {
            if (contains(o))
                return false;
            else
                return super.add(o);
        }
    };

    attachSwipe();

}

private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent e) {
        if (swipedPos < 0) return false;
        Point point = new Point((int) e.getRawX(), (int) e.getRawY());

        RecyclerView.ViewHolder swipedViewHolder = recyclerView.findViewHolderForAdapterPosition(swipedPos);
        View swipedItem = swipedViewHolder.itemView;
        Rect rect = new Rect();
        swipedItem.getGlobalVisibleRect(rect);

        if (e.getAction() == MotionEvent.ACTION_DOWN || e.getAction() == MotionEvent.ACTION_UP ||e.getAction() == MotionEvent.ACTION_MOVE)
        {
            if (rect.top < point.y && rect.bottom > point.y)
                gestureDetector.onTouchEvent(e);
            else {
                recoverQueue.add(swipedPos);
                swipedPos = -1;
                recoverSwipedItem();
            }
        }
        return false;
    }
};

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
    return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    int pos = viewHolder.getAdapterPosition();

    if (swipedPos != pos)
        recoverQueue.add(swipedPos);

    swipedPos = pos;

    if (buttonsBuffer.containsKey(swipedPos))
        buttons = buttonsBuffer.get(swipedPos);
    else
        buttons.clear();

    buttonsBuffer.clear();
    swipeThreshold = 0.5f * buttons.size() * BUTTON_WIDTH;
    recoverSwipedItem();

}

@Override
public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
    return swipeThreshold;
}

@Override
public float getSwipeEscapeVelocity(float defaultValue) {
    return 0.1f * defaultValue;
}

@Override
public float getSwipeVelocityThreshold(float defaultValue) {
    return 5.0f * defaultValue;
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive)
{
    int pos = viewHolder.getAdapterPosition();
    float translationX = dX;
    View itemView = viewHolder.itemView;

    if (pos < 0){
        swipedPos = pos;
        return;
    }

    if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){
        if(dX > 0) {
            List<SwipeHelperRight.UnderlayButton> buffer = new ArrayList<>();

            if (!buttonsBuffer.containsKey(pos)){
                instantiateUnderlayButton(viewHolder, buffer);
                buttonsBuffer.put(pos, buffer);
            }
            else {
                buffer = buttonsBuffer.get(pos);
            }

            translationX = dX * buffer.size() * BUTTON_WIDTH / itemView.getWidth();
            drawButtons(c, itemView, buffer, pos, translationX);
        }
    }

    super.onChildDraw(c, recyclerView, viewHolder, translationX, dY, actionState, isCurrentlyActive);
}



private void drawButtons(Canvas c, View itemView, List<SwipeHelperRight.UnderlayButton> buffer, int pos, float dX)
{
    float left = itemView.getLeft();
    float dButtonWidth = (-1) * dX / buffer.size();

    for (SwipeHelperRight.UnderlayButton button : buffer) {
        float right = left - dButtonWidth;
        button.onDraw(
                c,
                new RectF(
                        left,
                        itemView.getTop(),
                        right,
                        itemView.getBottom()
                ),
                pos
        );

        left = right;
    }
}


private synchronized void recoverSwipedItem(){
    while (!recoverQueue.isEmpty()){
        int pos = recoverQueue.poll();
        if (pos > -1) {
            recyclerView.getAdapter().notifyItemChanged(pos);
        }
    }
}
public void attachSwipe(){
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(this);
    itemTouchHelper.attachToRecyclerView(recyclerView);
}

public abstract void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<SwipeHelperRight.UnderlayButton> underlayButtons);

public static class UnderlayButton {
    private String text;
    private int imageResId;
    private int color;
    private int pos;

    Context context;
    private RectF clickRegion;
    private SwipeHelperRight.UnderlayButtonClickListener clickListener;

    public UnderlayButton(Context context,String text, int imageResId, int color, SwipeHelperRight.UnderlayButtonClickListener clickListener) {
        this.text = text;
        this.imageResId = imageResId;
        this.color = color;
        this.context = context;
        this.clickListener = clickListener;
    }

    public boolean onClick(float x, float y){
        if (clickRegion != null && clickRegion.contains(x, y)){
            clickListener.onClick(pos);
            return true;
        }

        return false;
    }

    public void onDraw(Canvas c, RectF rect, int pos){
        Paint p = new Paint();

        // Draw background
        p.setColor(color);
        c.drawRect(rect, p);



        // Draw Text
        p.setColor(Color.WHITE);
        p.setTextSize(25.0f);





        Rect r = new Rect();
        float cHeight = rect.height();
        float cWidth = rect.width();
        p.setTextAlign(Paint.Align.LEFT);
        p.getTextBounds(text, 0, text.length(), r);
        float x = cWidth / 2f - r.width() / 2f - r.left;
        float y = cHeight / 2f + r.height() / 2f - r.bottom;
        c.drawText(text, rect.left + x, rect.top + y + (r.height()*2), p);

        //Draw Image
        if (imageResId != 0){
            float textWidth = p.measureText(text);
            Bitmap bmp = drawableToBitmap(ContextCompat.getDrawable(context, imageResId));
            c.drawBitmap(bmp, rect.centerX()-(bmp.getWidth()/2f),rect.centerY()-(bmp.getHeight()/2f), null);
        }

        clickRegion = rect;
        this.pos = pos;
    }

    public static Bitmap drawableToBitmap (Drawable drawable) {
        Bitmap bitmap = null;

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }

}

public interface UnderlayButtonClickListener {
    void onClick(int pos);
}
}

然后在你的活动或片段中

SwipeHelperRight swipeHelperRight = new SwipeHelperRight(getContext(),binding.recyclerView) {
        @Override
        public void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<SwipeHelperRight.UnderlayButton> underlayButtons) {
            underlayButtons.add(new SwipeHelperRight.UnderlayButton(
                    getContext(),
                    "Archive",
                    R.drawable.ic_archive,
                    Color.parseColor("#BBBBC3"),
                    new SwipeHelperRight.UnderlayButtonClickListener() {
                        @Override
                        public void onClick(int pos) {
                            // TODO: onArchive
                            Toast.makeText(getContext(), "Archive", Toast.LENGTH_SHORT).show();
                        }
                    }
            ));

            underlayButtons.add(new SwipeHelperRight.UnderlayButton(
                    getContext(),
                    "Delete",
                    R.drawable.ic_delete,
                    Color.parseColor("#FE3B30"),
                    new SwipeHelperRight.UnderlayButtonClickListener() {
                        @Override
                        public void onClick(int pos) {
                            // TODO: onDelete
                            Toast.makeText(getContext(), "Delete", Toast.LENGTH_SHORT).show();
                        }
                    }
            ));
        }
    };

希望这对您有帮助


0
投票

感谢 Md Saif 提供 SwipeHelperRight 类

我将添加我自己制作的 SwipeHelperLeft 类的代码,因为我需要从左向右滑动

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public abstract class SwipeHelper extends ItemTouchHelper.SimpleCallback {


public static final int BUTTON_WIDTH = 200;
private RecyclerView recyclerView;
private List<SwipeHelper.UnderlayButton> buttons;
private GestureDetector gestureDetector;
private int swipedPos = -1;
private float swipeThreshold = 0.5f;
private Map<Integer, List<SwipeHelper.UnderlayButton>> buttonsBuffer;
private Queue<Integer> recoverQueue;

private GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener(){
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        for (SwipeHelper.UnderlayButton button : buttons){
            if(button.onClick(e.getX(), e.getY()))
                break;
        }

        return true;
    }
};

public SwipeHelper(Context context, RecyclerView recyclerView) {
    super(0, ItemTouchHelper.LEFT);
    this.recyclerView = recyclerView;
    this.buttons = new ArrayList<>();
    this.gestureDetector = new GestureDetector(context, gestureListener);
    this.recyclerView.setOnTouchListener(onTouchListener);
    buttonsBuffer = new HashMap<>();
    recoverQueue = new LinkedList<Integer>(){
        @Override
        public boolean add(Integer o) {
            if (contains(o))
                return false;
            else
                return super.add(o);
        }
    };

    attachSwipe();

}

private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent e) {
        if (swipedPos < 0) return false;
        Point point = new Point((int) e.getRawX(), (int) e.getRawY());

        RecyclerView.ViewHolder swipedViewHolder = recyclerView.findViewHolderForAdapterPosition(swipedPos);
        View swipedItem = swipedViewHolder.itemView;
        Rect rect = new Rect();
        swipedItem.getGlobalVisibleRect(rect);

        if (e.getAction() == MotionEvent.ACTION_DOWN || e.getAction() == MotionEvent.ACTION_UP ||e.getAction() == MotionEvent.ACTION_MOVE)
        {
            if (rect.top < point.y && rect.bottom > point.y)
                gestureDetector.onTouchEvent(e);
            else {
                recoverQueue.add(swipedPos);
                swipedPos = -1;
                recoverSwipedItem();
            }
        }
        return false;
    }
};

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
    return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    int pos = viewHolder.getAdapterPosition();

    if (swipedPos != pos)
        recoverQueue.add(swipedPos);

    swipedPos = pos;

    if (buttonsBuffer.containsKey(swipedPos))
        buttons = buttonsBuffer.get(swipedPos);
    else
        buttons.clear();

    buttonsBuffer.clear();
    swipeThreshold = 0.5f * buttons.size() * BUTTON_WIDTH;
    recoverSwipedItem();

}

@Override
public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
    return swipeThreshold;
}

@Override
public float getSwipeEscapeVelocity(float defaultValue) {
    return 0.1f * defaultValue;
}

@Override
public float getSwipeVelocityThreshold(float defaultValue) {
    return 5.0f * defaultValue;
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive)
{
    int pos = viewHolder.getAdapterPosition();
    float translationX = dX;
    View itemView = viewHolder.itemView;

    if (pos < 0){
        swipedPos = pos;
        return;
    }

    if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){
        if(dX < 0) {
            List<SwipeHelper.UnderlayButton> buffer = new ArrayList<>();

            if (!buttonsBuffer.containsKey(pos)){
                instantiateUnderlayButton(viewHolder, buffer);
                buttonsBuffer.put(pos, buffer);
            }
            else {
                buffer = buttonsBuffer.get(pos);
            }

            translationX = dX * buffer.size() * BUTTON_WIDTH / itemView.getWidth();
            drawButtons(c, itemView, buffer, pos, translationX);
        }
    }

    super.onChildDraw(c, recyclerView, viewHolder, translationX, dY, actionState, isCurrentlyActive);
}



private void drawButtons(Canvas c, View itemView, List<SwipeHelper.UnderlayButton> buffer, int pos, float dX)
{
    float right = itemView.getRight();
    float dButtonWidth = dX / buffer.size();

    for (SwipeHelper.UnderlayButton button : buffer) {
        float left = right + dButtonWidth;
        button.onDraw(
                c,
                new RectF(
                        left,
                        itemView.getTop(),
                        right,
                        itemView.getBottom()
                ),
                pos
        );

        right = left;
    }
}


private synchronized void recoverSwipedItem(){
    while (!recoverQueue.isEmpty()){
        int pos = recoverQueue.poll();
        if (pos > -1) {
            recyclerView.getAdapter().notifyItemChanged(pos);
        }
    }
}
public void attachSwipe(){
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(this);
    itemTouchHelper.attachToRecyclerView(recyclerView);
}

public abstract void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<SwipeHelper.UnderlayButton> underlayButtons);

public static class UnderlayButton {
    private String text;
    private int imageResId;
    private int color;
    private int pos;

    Context context;
    private RectF clickRegion;
    private SwipeHelper.UnderlayButtonClickListener clickListener;

    public UnderlayButton(Context context,String text, int imageResId, int color, SwipeHelper.UnderlayButtonClickListener clickListener) {
        this.text = text;
        this.imageResId = imageResId;
        this.color = color;
        this.context = context;
        this.clickListener = clickListener;
    }

    public boolean onClick(float x, float y){
        if (clickRegion != null && clickRegion.contains(x, y)){
            clickListener.onClick(pos);
            return true;
        }

        return false;
    }

    public void onDraw(Canvas c, RectF rect, int pos){
        Paint p = new Paint();

        // Draw background
        p.setColor(color);
        c.drawRect(rect, p);



        // Draw Text
        p.setColor(Color.WHITE);
        p.setTextSize(25.0f);





        Rect r = new Rect();
        float cHeight = rect.height();
        float cWidth = rect.width();
        p.setTextAlign(Paint.Align.LEFT);
        p.getTextBounds(text, 0, text.length(), r);
        float x = cWidth / 2f - r.width() / 2f - r.left;
        float y = cHeight / 2f + r.height() / 2f - r.bottom;
        c.drawText(text, rect.left + x, rect.top + y + (r.height()*2), p);

        //Draw Image
        if (imageResId != 0){
            float textWidth = p.measureText(text);
            Bitmap bmp = drawableToBitmap(ContextCompat.getDrawable(context, imageResId));
            c.drawBitmap(bmp, rect.centerX()-(bmp.getWidth()/2f),rect.centerY()-(bmp.getHeight()/2f), null);
        }

        clickRegion = rect;
        this.pos = pos;
    }

    public static Bitmap drawableToBitmap (Drawable drawable) {
        Bitmap bitmap = null;

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }

}

public interface UnderlayButtonClickListener {
    void onClick(int pos);
}
}
© www.soinside.com 2019 - 2024. All rights reserved.