1

我正在尝试根据这篇文章在 android 中实现滑动操作:[ https://codeburst.io/android-swipe-menu-with-recyclerview-8f28a235ff28]

我正在尝试实现这样一种方式,即当用户在关闭项目上滑动时,它将关闭打开的项目并滑动新的项目。我已经用了 2 天了,非常感谢这里的帮助。

谢谢

enum ButtonsState {
GONE,
LEFT_VISIBLE,
RIGHT_VISIBLE
}

class SwipeController extends ItemTouchHelper.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) {
    if (viewHolder instanceof PendingOrRecentItemViewHolder) {
        return makeMovementFlags(0, /*LEFT |*/RIGHT);
    } else {
        return makeMovementFlags(0, 0 /*LEFT |*/);
    }
}

@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, RecyclerView.ViewHolder viewHolder, 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);
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        } else {
            setTouchListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }
    }
    if (buttonShowedState == ButtonsState.GONE) {
        super.onChildDraw(c, recyclerView, viewHolder, dX / 2, dY, actionState, isCurrentlyActive);
    }
    currentItemViewHolder = viewHolder;
    drawButtons(c, 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.LEFT_VISIBLE;

                if (buttonShowedState != ButtonsState.GONE) {
                    setTouchDownListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
                    setItemsClickable(recyclerView, false);
                }
            }
            return false;
        }
    });
}

private void setTouchDownListener(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) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                setTouchUpListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                setTouchUpListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }
            return false;
        }
    });
}

private void setTouchUpListener(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) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                SwipeController.super.onChildDraw(c, recyclerView, viewHolder, 0F, dY, actionState, 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.onRightClicked(viewHolder.getAdapterPosition());
                    }
                }
                if (currentItemViewHolder != null) {
                    clearView(recyclerView, currentItemViewHolder);
                }
                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, corners, p);
    drawText("EDIT", c, leftButton, p);

    buttonInstance = null;
    if (buttonShowedState == ButtonsState.LEFT_VISIBLE) {
        buttonInstance = leftButton;
    }

}

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 onDraw(Canvas c, RecyclerView recyclerView) {
    if (currentItemViewHolder != null) {
        drawButtons(c, currentItemViewHolder);
    }

}

public RecyclerView.ViewHolder getCurrentItemViewHolder() {
    return currentItemViewHolder;
}

public ButtonsState getButtonShowedState() {
    return buttonShowedState;
}

public void clearData(RecyclerView mRecyclerView) {
    currentItemViewHolder.itemView.performClick();
}
}
4

0 回答 0