英文:
How can I implement swipe-to-delete gesture, exactly as shown in Material Design's documentation?
问题
我想为我的ListView实现滑动删除手势,Material Design文档显示可以实现(参见这里)。是否有一种简单直接的方法来实现这个,或者我应该实现一个自定义类来扩展ItemTouchHelper.SimpleCallback
以添加这个行为?
英文:
I want to implement a swipe-to-delete gesture for my ListView, and Material Design docs show it can be done (see here). Is there a straightforward way of doing this, or am I meant to implement a custom class which extends ItemTouchHelper.SimpleCallback
to add this behavior?
答案1
得分: 0
从外表看,您可以使用另一个活动来显示信息。要执行有关它应该从哪里开始的过渡动画,您可以像这样获取坐标。
从列表视图(这是在适配器中调用的子视图,我的意思是containerView
)
Rect rect = new Rect();
child.getGlobalVisibleRect(rect);
上面的代码将为您提供视图在窗口中的位置,包括左、上、右、下坐标。
在打开活动时,您可以像这样创建意图:
Intent intent = new Intent(context, DetailsActivity.class);
// 这就是您上面获取的"rect",视图在屏幕上的坐标。
intent.setSourceBounds(rect);
// 通过Intent.putExtra()传递必要的元素
// 这些将包含产品的信息。
// 不要在这里传递图像,只传递图像引用。
intent.putExtra("name", product.name);
// 然后正常启动活动。
context.startActivity(intent);
这应该执行您在链接中看到的动画。(顺便说一句,如果有更简单的方法,我不知道是什么。)
英文:
From the looks of it, you can use another activity to display the information. To perform the transition animation about where it should start, you can do something like this to get the coordinates.
From list view (this is the child view that is called in the adapter, I mean, containerView
)
Rect rect = new Rect();
child.getGlobalVisibleRect(rect);
The code above will give you the view's position in window, with both left, top, right, bottom coordinates.
While opening the activity, you can create the intent like this:
Intent intent = new Intent(context, DetailsActivity.class);
// This is the "rect" you got above, the view's coordinates in screen.
intent.setSourceBounds(rect);
// Pass necessary elements through Intent.putExtra()
// These will contain the product's information.
// DO NOT PASS IMAGES HERE, ONLY PASS IMAGE REFERENCES.
intent.putExtra("name", product.name);
// Then start the activity normally.
context.startActivity(intent);
This should perform the animation that you see on that link. (If there is an easier way, I don't know what it is, by the way.)
专注分享java语言的经验与见解,让所有开发者获益!
评论