RecyclerView ItemTouchHelper 滑动时两个按钮

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

按照这个问题 RecyclerView ItemTouchHelper 滑动按钮 (不一样)。我想在每一侧添加按钮。

该链接的官方答案仅适用于一侧。

我尝试过的:

public abstract class SwipeHelper extends ItemTouchHelper.SimpleCallback {

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

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

        return true;
    }
};

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

        return true;
    }
};

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);
                gestureDetector2.onTouchEvent(e);
            }
            //else if(rect.top > point.y && rect.bottom < point.y){                }
            else {
                recoverQueue.add(swipedPos);
                swipedPos = -1;
                recoverSwipedItem();
            }
        }
        return false;
    }
};

public SwipeHelper(Context context, RecyclerView recyclerView) {
    super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
    this.recyclerView = recyclerView;
    this.buttons = new ArrayList<>();
    this.gestureDetector = new GestureDetector(context, gestureListener);
    this.gestureDetector2 = new GestureDetector(context, gestureListener2);
    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();
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, 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<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);
        }
        if(dX > 0) {
            List<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 synchronized void recoverSwipedItem(){
    while (!recoverQueue.isEmpty()){
        int pos = recoverQueue.poll();
        if (pos > -1) {
            recyclerView.getAdapter().notifyItemChanged(pos);
        }
    }
}

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

    for (UnderlayButton button : buffer) {
        if (dX < 0) {
            left = right - dButtonWidth;
            button.onDraw(
                    c,
                    new RectF(
                            left,
                            itemView.getTop(),
                            right,
                            itemView.getBottom()
                    ),
                    pos, dX 
            );
            right = left;
        } else if (dX > 0) {
            right = left - dButtonWidth;
            button.onDraw(c,
                    new RectF(
                            right,
                            itemView.getTop(),
                            left,
                            itemView.getBottom()
                    ), pos, dX 
            );
            left = right;
        }
    }
}
public void attachSwipe(){
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(this);
    itemTouchHelper.attachToRecyclerView(recyclerView);
}

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

public static class UnderlayButton {
    private String text;
    private int imageResId;
    private int color;
    private UnderlayButtonClickListener clickListener;
    private String text2;
    private int imageResId2;
    private int color2;
    private UnderlayButtonClickListener2 clickListener2;
    private RectF clickRegion;
    private RectF clickRegion2;
    private int pos;

    public UnderlayButton(String text, int imageResId, int color, UnderlayButtonClickListener clickListener, String text2, int imageResId2, int color2, UnderlayButtonClickListener2 clickListener2) {
        this.text = text;
        this.imageResId = imageResId;
        this.color = color;
        this.clickListener = clickListener;
        this.text2 = text2;
        this.imageResId2 = imageResId2;
        this.color2 = color2;
        this.clickListener2 = clickListener2;
    }

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

    public boolean onClick2(float x, float y){
        if (clickRegion2 != null && clickRegion2.contains(x, y)){
            clickListener2.onClick(pos);
            return true;
        }
        return false;
    }

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

        // Draw background

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {


            if (dX < 0) {

                //Boton derecho
                c.drawRect(rect, p);
                p.setColor(color);

            }
            else{
                //Boton Izquierdo
                p.setColor(color2);
                c.drawRect(rect, p);
            }

            clickRegion = rect;
            clickRegion2 = rect;
            this.pos = pos;
        }

    }
}

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

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

我尝试向“UnderlayButton”类添加更多参数,因为在我尝试单独使用这两个类之前,但它给我带来了 OnClickListener 的问题。

所以我发现最好的方法是在“UnderlayButton”类中添加额外的参数。不过,它对我来说不起作用。

有人可以帮助我吗?

java android-studio android-recyclerview swipe itemtouchhelper
1个回答
0
投票

有人有答案吗?我通过 Kotlin 方法遇到了这个问题。滑动并单击一个按钮后,我无法在其他项目上执行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.