Android-滑动以删除-添加撤消选项
我使用的是Android-滑动以删除-添加撤消选项,android,Android,我使用的是RecyclerView,我已成功使用此代码实现了刷卡删除: @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { if (actionState == It
RecyclerView
,我已成功使用此代码实现了刷卡删除:
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
// Get RecyclerView item from the ViewHolder
View itemView = viewHolder.itemView;
canvas = c;
Paint p = new Paint();
Bitmap icon;
if (dX > 0) {
icon = BitmapFactory.decodeResource(
activity.getResources(), R.drawable.ic_delete_white_36dp);
/* Set your color for positive displacement */
p.setARGB(255, 255, 0, 0);
// Draw Rect with varying right side, equal to displacement dX
c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX,
(float) itemView.getBottom(), p);
// Set the image icon for Right swipe
c.drawBitmap(icon,
(float) itemView.getLeft() + HelperMethods.dpToPixel(16),
(float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - icon.getHeight()) / 2,
p);
} else {
icon = BitmapFactory.decodeResource(
activity.getResources(), R.drawable.ic_delete_white_36dp);
/* Set your color for negative displacement */
p.setARGB(255, 255, 0, 0);
// Draw Rect with varying left side, equal to the item's right side
// plus negative displacement dX
c.drawRect((float) itemView.getRight() + dX, (float) itemView.getTop(),
(float) itemView.getRight(), (float) itemView.getBottom(), p);
//Set the image icon for Left swipe
c.drawBitmap(icon,
(float) itemView.getRight() - HelperMethods.dpToPixel(16) - icon.getWidth(),
(float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - icon.getHeight()) / 2,
p);
}
Integer ALPHA_FULL = 255;
// Fade out the view as it is swiped out of the parent's bounds
final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
viewHolder.itemView.setAlpha(alpha);
viewHolder.itemView.setTranslationX(dX);
}
}
在用户刷卡一个项目后的几秒钟内,我无法实现撤销选项
我希望它看起来像gmail应用程序撤销选项,我不想使用任何外部库。我猜你有一个
onsweep
方法,从适配器中删除该项?我的建议是不要把它移到那里,只要把它标记为被刷掉就行了。适配器和视图保持架应在OnChildRaw中显示与后面的图形相同的视图。这里有一个撤销按钮,所以如果用户点击它,只需将swiked off标志设置为false并重新绘制行(我的意思是通知适配器这样做)。在onsweed
中启动计时器,如果在x毫秒内没有任何事情发生,则实际从适配器中删除该项
编辑:这只是一个想法,实际上从来没有这样做过
编辑:我试过了,看看这个和这个