具有自定义滑动视图的Android Recycler

问题描述

我正在尝试为recyclerview添加自定义滑动视图。以下代码运行良好。从this link获得。在此代码中,按钮是通过onDraw方法创建的。

enum Buttonsstate {
    GONE,LEFT_VISIBLE,RIGHT_VISIBLE
}

class SwipeController extends Callback {

    private boolean swipeBack = false;

    private Buttonsstate buttonShowedState = Buttonsstate.GONE;

    private RectF buttonInstance = null;

    private RecyclerView.ViewHolder currentItemViewHolder = null;

    private SwipeControllerActions buttonsActions = null;

    private static final float buttonWidth = 300;

    public SwipeController(SwipeControllerActions buttonsActions) {
        this.buttonsActions = buttonsActions;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(0,LEFT | RIGHT);
    }

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

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder,int direction) {

    }

    @Override
    public int convertToAbsoluteDirection(int flags,int layoutDirection) {
        if (swipeBack) {
            swipeBack = buttonShowedState != Buttonsstate.GONE;
            return 0;
        }
        return super.convertToAbsoluteDirection(flags,layoutDirection);
    }

    @Override
    public void onChildDraw(Canvas c,RecyclerView recyclerView,float dX,float dY,int actionState,boolean isCurrentlyActive) {
        if (actionState == ACTION_STATE_SWIPE) {
            if (buttonShowedState != Buttonsstate.GONE) {
                if (buttonShowedState == Buttonsstate.LEFT_VISIBLE) dX = Math.max(dX,buttonWidth);
                if (buttonShowedState == Buttonsstate.RIGHT_VISIBLE) dX = Math.min(dX,-buttonWidth);
                super.onChildDraw(c,recyclerView,viewHolder,dX,dY,actionState,isCurrentlyActive);
            }
            else {
                setTouchListener(c,isCurrentlyActive);
            }
        }

        if (buttonShowedState == Buttonsstate.GONE) {
            super.onChildDraw(c,isCurrentlyActive);
        }
        currentItemViewHolder = viewHolder;
    }

    private void setTouchListener(final Canvas c,final RecyclerView recyclerView,final RecyclerView.ViewHolder viewHolder,final float dX,final float dY,final int actionState,final boolean isCurrentlyActive) {
        recyclerView.setonTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v,MotionEvent event) {

                swipeBack = event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP;

                if (swipeBack) {
                    if (dX < -buttonWidth) buttonShowedState = Buttonsstate.RIGHT_VISIBLE;
                    else if (dX > buttonWidth) buttonShowedState  = Buttonsstate.LEFT_VISIBLE;

                    if (buttonShowedState != Buttonsstate.GONE) {
                        setTouchDownListener(c,isCurrentlyActive);
                        setItemsClickable(recyclerView,false);
                    }
                }
                return false;
            }
        });
    }

    private void setTouchDownListener(final Canvas c,MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {

                    setTouchUpListener(c,isCurrentlyActive);
                }
                return false;
            }
        });
    }

    private void setTouchUpListener(final Canvas c,MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    SwipeController.super.onChildDraw(c,0F,isCurrentlyActive);
                    recyclerView.setonTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v,MotionEvent event) {
                            return false;
                        }
                    });
                    setItemsClickable(recyclerView,true);
                    swipeBack = false;

                    if (buttonsActions != null && buttonInstance != null && buttonInstance.contains(event.getX(),event.getY())) {
                        if (buttonShowedState == Buttonsstate.LEFT_VISIBLE) {
                            buttonsActions.onLeftClicked(viewHolder.getAdapterPosition());
                        }
                        else if (buttonShowedState == Buttonsstate.RIGHT_VISIBLE) {
                            buttonsActions.onRightClicked(viewHolder.getAdapterPosition());
                        }
                    }
                    buttonShowedState = Buttonsstate.GONE;
                    currentItemViewHolder = null;
                }
                return false;
            }
        });
    }

    private void setItemsClickable(RecyclerView recyclerView,boolean isClickable) {
        for (int i = 0; i < recyclerView.getChildCount(); ++i) {
            recyclerView.getChildAt(i).setClickable(isClickable);
        }
    }

    private void drawButtons(Canvas c,RecyclerView.ViewHolder viewHolder) {
        float buttonWidthWithoutPadding = buttonWidth - 20;
        float corners = 16;

        View itemView = viewHolder.itemView;
        Paint p = new Paint();

        RectF leftButton = new RectF(itemView.getLeft(),itemView.getTop(),itemView.getLeft() + buttonWidthWithoutPadding,itemView.getBottom());
        p.setColor(Color.BLUE);
        c.drawRoundRect(leftButton,corners,p);
        drawText("EDIT",c,leftButton,p);

        RectF rightButton = new RectF(itemView.getRight() - buttonWidthWithoutPadding,itemView.getRight(),itemView.getBottom());
        p.setColor(Color.RED);
        c.drawRoundRect(rightButton,p);
        drawText("DELETE",rightButton,p);

        buttonInstance = null;
        if (buttonShowedState == Buttonsstate.LEFT_VISIBLE) {
            buttonInstance = leftButton;
        }
        else if (buttonShowedState == Buttonsstate.RIGHT_VISIBLE) {
            buttonInstance = rightButton;
        }
    }

    private void drawText(String text,Canvas c,RectF button,Paint p) {
        float textSize = 60;
        p.setColor(Color.WHITE);
        p.setAntiAlias(true);
        p.setTextSize(textSize);

        float textWidth = p.measureText(text);
        c.drawText(text,button.centerX()-(textWidth/2),button.centerY()+(textSize/2),p);
    }

    public void onDrawing(Canvas c) {
        if (currentItemViewHolder != null) {
            drawButtons(c,currentItemViewHolder);
        }
    }
}

但是我想将视图附加到滑动上。所以我修改了如下代码。我可以向右滑动此代码。但是,如果我再次单击或滑动,则滑动的项目不会关闭。还有一些时间视图从recyclerview中退出。请帮助我。

public class SwipetouchListener extends itemtouchhelper.Callback {

    private boolean swipeBack = false;
    private boolean allowSwipeLeft,allowSwipeRight;
    private List<Integer> unSwipablePosition = new ArrayList<>();
    private Buttonsstate buttonShowedState = Buttonsstate.GONE;
    private static final float buttonWidth = 300;
    private SwipeControllerActions buttonsActions = null;
    private RectF buttonInstance = null;
    private View foregroundView = null;

    public void setSwipeDirection(boolean allowSwipeLeft,boolean allowSwipeRight){
        this.allowSwipeLeft = allowSwipeLeft;
        this.allowSwipeRight = allowSwipeRight;
    }

    public void setUnSwipeableRows(Integer... rows) {
        this.unSwipablePosition = new ArrayList<>(Arrays.asList(rows));
    }

    private int getSwipeFlags(){

        if(allowSwipeRight && allowSwipeLeft)
            return LEFT|RIGHT;
        else if(allowSwipeLeft)
            return LEFT;
        else if(allowSwipeRight)
            return RIGHT;
        return 0;

    }

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView,@NonNull RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(0,getSwipeFlags());
    }

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

        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder,int direction) {
    }

    @Override
    public int convertToAbsoluteDirection(int flags,int layoutDirection) {
        if (swipeBack) {
            swipeBack = false;
            return 0;
        }
        return super.convertToAbsoluteDirection(flags,boolean isCurrentlyActive) {
        int currentPosition = ((RecyclerAdapter.MyViewHolder) viewHolder).getAdapterPosition();
        if(unSwipablePosition.contains(currentPosition))
            return;
        
        foregroundView = ((RecyclerAdapter.MyViewHolder) viewHolder).viewForeground;
        if (actionState == ACTION_STATE_SWIPE) {
            if (buttonShowedState != Buttonsstate.GONE) {
                if (buttonShowedState == Buttonsstate.LEFT_VISIBLE) dX = Math.max(dX,-buttonWidth);
                //Adding foreground layout
                getDefaultUIUtil().onDraw(c,foregroundView,isCurrentlyActive);
            } else {
                setTouchListener(c,isCurrentlyActive);
            }
        }
        if (buttonShowedState == Buttonsstate.GONE) {
            //Adding foreground layout
            getDefaultUIUtil().onDraw(c,isCurrentlyActive);
        }

    }

    private void setTouchListener(final Canvas c,MotionEvent event) {
                swipeBack = event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP;
                if (swipeBack) {
                    if (dX < -buttonWidth) buttonShowedState = Buttonsstate.RIGHT_VISIBLE;
                    else if (dX > buttonWidth) buttonShowedState  = Buttonsstate.LEFT_VISIBLE;

                    if (buttonShowedState != Buttonsstate.GONE) {
                        setTouchDownListener(c,MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    setTouchUpListener(c,MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    //Adding foreground layout
                    getDefaultUIUtil().onDraw(c,false);
                    recyclerView.setonTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v,MotionEvent event) {
                            return false;
                        }
                    });

                    if (buttonsActions != null && buttonInstance != null && buttonInstance.contains(event.getX(),event.getY())) {
                        if (buttonShowedState == Buttonsstate.LEFT_VISIBLE) {
                            buttonsActions.onLeftClicked(viewHolder.getAdapterPosition());
                        }
                        else if (buttonShowedState == Buttonsstate.RIGHT_VISIBLE) {
                            buttonsActions.onRightClicked(viewHolder.getAdapterPosition());
                        }
                    }

                    setItemsClickable(recyclerView,true);
                    swipeBack = false;
                    buttonShowedState = Buttonsstate.GONE;
                }
                return false;
            }
        });
    }

Output Gif

解决方法

似乎您正在尝试重新发明轮子:) 请看看那个AndroidSwipeLayou并适应自己。我在大多数项目中都使用了它,效果很好。