RecyclerView ItemTouchHelper-如何撤消或重置刷卡?了解如何删除背景或图标

时间:2019-01-09 07:27:20

标签: android android-recyclerview itemtouchhelper

在Snackbar上单击撤消后,我试图撤消刷卡操作(将background.setboundsxmark.setbounds重置为0,0,0,0)。

如果clearView的边界设置为0,0,0,0,我尝试过onChildDraw或尝试在viewBeingCleared == true中引入布尔值,但是它仍然不起作用。

单击撤消后,如何重置视图或删除红色背景和图标?

一些帮助将不胜感激。

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

    View view = viewHolder.itemView;
    final TextView orderID = (TextView) view.findViewById(R.id.order_ID);

    // showing snack bar with Undo option

    Snackbar.make(view, "removed from cart!", 3000)
            .addCallback(new Snackbar.Callback() {
                @Override
                public void onDismissed(Snackbar snackbar1, int event) {
                    if (event == Snackbar.Callback.DISMISS_EVENT_TIMEOUT) {
                        // Snackbar closed on its own
                        mAdapter.deleteItem(position, orderID.getText().toString());
                    }
                }

            })
            .setActionTextColor(Color.YELLOW)
            .setAction("UNDO", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // undo is selected, restore the deleted item
                    //mAdapter.restoreItem(deletedItem, deletedIndex);
                    viewBeingCleared = true;
                    onClicked = true;
                    Log.d(TAG, "Undo clicked");
                }
            })
            .show();
}

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    super.clearView(recyclerView, viewHolder);
    background.setBounds(0,0,0,0);
    xMark.setBounds(0,0,0,0);
    viewBeingCleared = true;
}

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

    // not sure why, but this method get's called for viewholder that are already swiped away
    if (viewHolder.getAdapterPosition() == -1) {
        // not interested in those
        return;
    }

    if (!initiated) {
        init();
    }

    if (viewBeingCleared) {
        viewBeingCleared=false;
    } else {
        // draw red background
        background.setBounds(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
        background.draw(c);

        // draw x mark
        int itemHeight = itemView.getBottom() - itemView.getTop();
        int intrinsicWidth = xMark.getIntrinsicWidth();
        int intrinsicHeight = xMark.getIntrinsicWidth();

        int xMarkLeft = itemView.getRight() - xMarkMargin - intrinsicWidth;
        int xMarkRight = itemView.getRight() - xMarkMargin;
        int xMarkTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
        int xMarkBottom = xMarkTop + intrinsicHeight;


        xMark.setBounds(xMarkLeft, xMarkTop, xMarkRight, xMarkBottom);
        xMark.draw(c);

    }

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

Looped not working undo

0 个答案:

没有答案