在Snackbar上单击撤消后,我试图撤消刷卡操作(将background.setbounds
和xmark.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);
}