在 Snackbar 上单击撤消后,我正在尝试撤消滑动(重置background.setbounds
和0,0,0,0)。xmark.setbounds
我已经尝试clearView
或尝试在onChildDraw
if viewBeingCleared == true
then setbounds to 0,0,0,0 中引入一个布尔值,但它仍然不起作用。
单击撤消后如何重置视图或删除红色背景和图标?
一些帮助将不胜感激。
@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);
}