以编程方式 "swipe-to-dismiss" ListView/RecyclerView 中的项目?
Programmatically "swipe-to-dismiss" an item in a ListView/RecyclerView?
我需要能够以编程方式关闭 RecyclerView
中的项目,而无需用户实际滑动(相反,我想在他们点击卡片中的按钮时关闭该项目)。我见过的很多库似乎只支持实际滑动。
我试过使用现有的库,只是通过以编程方式自行创建滑动来模拟 MotionEvent
,但这会干扰另一个水平滑动侦听器,所以我想知道这还能怎么办完成,理想情况下是 RecyclerView
但如果有人知道如何为 ListView
而不是我可以尝试适应它。
我查看了 this 库以及其他库以获取灵感,但我不知道如何以编程方式触发滑动。
使用带有自定义适配器的 ListView
或 RecyclerView
,并在从数据列表中删除项目后调用 notifyDataSetChanged
:
private void removeListItem(View rowView, final int position) {
Animation anim = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
anim.setDuration(500);
rowView.startAnimation(anim);
new Handler().postDelayed(new Runnable() {
public void run() {
values.remove(position); //Remove the current content from the array
adapter.notifyDataSetChanged(); //Refresh list
}
}, anim.getDuration());
}
使用提供滑动功能的库之一来提取动画部分,如果我没记错的话它在 action_up 在 onTouch()。然后从按钮的 onClick 中调用它。
我需要能够以编程方式关闭 RecyclerView
中的项目,而无需用户实际滑动(相反,我想在他们点击卡片中的按钮时关闭该项目)。我见过的很多库似乎只支持实际滑动。
我试过使用现有的库,只是通过以编程方式自行创建滑动来模拟 MotionEvent
,但这会干扰另一个水平滑动侦听器,所以我想知道这还能怎么办完成,理想情况下是 RecyclerView
但如果有人知道如何为 ListView
而不是我可以尝试适应它。
我查看了 this 库以及其他库以获取灵感,但我不知道如何以编程方式触发滑动。
使用带有自定义适配器的 ListView
或 RecyclerView
,并在从数据列表中删除项目后调用 notifyDataSetChanged
:
private void removeListItem(View rowView, final int position) {
Animation anim = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
anim.setDuration(500);
rowView.startAnimation(anim);
new Handler().postDelayed(new Runnable() {
public void run() {
values.remove(position); //Remove the current content from the array
adapter.notifyDataSetChanged(); //Refresh list
}
}, anim.getDuration());
}
使用提供滑动功能的库之一来提取动画部分,如果我没记错的话它在 action_up 在 onTouch()。然后从按钮的 onClick 中调用它。