[英]How to swipe to delete a Card (using appcompat v7's CardView)
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:stateListAnimator="@anim/anim"
android:layout_margin="5dp"
android:clickable="true"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textview"
android:minHeight="?android:listPreferredItemHeight"
android:gravity="center_vertical">
</TextView>
</android.support.v7.widget.CardView>
I'm using CardView to display a row of texts. 我正在使用CardView显示一行文本。 How do I swipe to delete those rows -- which are cardviews?
如何滑动以删除那些行 - 哪些是cardviews? Also, how to set an onItemClickListener for each row?
另外,如何为每一行设置onItemClickListener? Again, I'm using cardview to display each row.
我再次使用cardview来显示每一行。
I wanted to do something similar, so I adapted romannurik's Android-SwipeToDismiss to do exactly what we wanted. 我想做类似的事情,所以我调整了romannurik的Android-SwipeToDismiss来完成我们想要的。
The code is on github with a working sample application, and consists of: 代码在github上,带有一个工作示例应用程序,包括:
RecyclerView.OnItemTouchListener
that listens to touch events and detects when an item is being swiped, animating it accordingly. RecyclerView.OnItemTouchListener
的子类,用于侦听触摸事件并检测项目何时被刷过,并相应地设置动画。 SwipeListener
that is called in order to know if an item can be dismissed and called again when items are dismissed. SwipeListener
,以便在项目被解除时知道项目是否可以被解除并再次调用。 To use it, follow the instructions on github, or just copy the class SwipeableRecyclerViewTouchListener
to your project and use it like this: 要使用它,请按照github上的说明操作,或者只是将类
SwipeableRecyclerViewTouchListener
复制到您的项目中并使用它如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mItems = new ArrayList<>(30);
for (int i = 0; i < 30; i++) {
mItems.add(String.format("Card number %2d", i));
}
mAdapter = new CardViewAdapter(mItems);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter);
SwipeableRecyclerViewTouchListener swipeTouchListener =
new SwipeableRecyclerViewTouchListener(mRecyclerView,
new SwipeableRecyclerViewTouchListener.SwipeListener() {
@Override
public boolean canSwipe(int position) {
return true;
}
@Override
public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
mItems.remove(position);
mAdapter.notifyItemRemoved(position);
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
mItems.remove(position);
mAdapter.notifyItemRemoved(position);
}
mAdapter.notifyDataSetChanged();
}
});
mRecyclerView.addOnItemTouchListener(swipeTouchListener);
}
Here is the famous Swipe to Dismiss example from Roman Nurik. 以下是Roman Nurik着名的Swipe to Dismiss示例。
https://github.com/romannurik/Android-SwipeToDismiss https://github.com/romannurik/Android-SwipeToDismiss
It includes dismissing items in a list and dismissing separate View
s. 它包括解雇列表中的项目并解除单独的
View
。 It should work on any View
including CardView
. 它应该适用于包括
CardView
在内的任何View
。
SwipeDismissListViewTouchListener
is for using in a ListView
to swipe items. SwipeDismissListViewTouchListener
用于在ListView
使用滑动项目。 SwipeDismissTouchListener
is for any View
to dismiss the whole View
completely. SwipeDismissTouchListener
适用于任何View
完全关闭整个View
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.