Android单击按钮即可删除所有带有动画的recyclerview [英] Android remove all recyclerview with animation on a button click
问题描述
我有一个回收站视图。在按钮上单击我想从recyclerview中删除所有项目,但是必须使用动画删除这些项目。
我可以一次删除所有项目,但是我不知道如何使用动画删除它们。谢谢
I have a recycler view. On a button click I want to remove all the items from the recyclerview but the items must be removed with animation. I am able to remove all the items at once but I don't know how to remove them with animation. Thanks
推荐答案
它很旧,但是希望它对其他人有所帮助,因为它尚未得到答复。我通过模拟一个项目的一次滑动动画来一次删除一个项目,并在删除下一个项目之前发布了一个延迟,依此类推直到的最后一个项目RecyclerView
It's old, but wish this helps someone else as it's already not answered yet; I have done it by deleting a single item at a time by simulating a swipe animation on this item, and post a delay before deleting the next item, and so on to the way down to the last item of the RecyclerView
第1步:
在包含清除所有按钮和 RecyclerView
实例的活动中:创建删除单个项目的方法
In your activity that holds the clear all button and the RecyclerView
instance: Create a method of single item delete
private void deleteItem(View rowView, final int position) {
Animation anim = AnimationUtils.loadAnimation(requireContext(),
android.R.anim.slide_out_right);
anim.setDuration(300);
rowView.startAnimation(anim);
new Handler().postDelayed(new Runnable() {
public void run() {
if (myDataSource.size() == 0) {
addEmptyView(); // adding empty view instead of the RecyclerView
return;
}
myDataSource.remove(position); //Remove the current content from the array
myRVAdapter.notifyDataSetChanged(); //Refresh list
}
}, anim.getDuration());
}
第二步:
创建将删除所有 RecyclerView
列表项的方法>>在按钮单击回调中调用它。
Create the method that will delete all RecyclerView
list items >> call it in your button click callback.
boolean mStopHandler = false;
private void deleteAllItems() {
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
if (myDataSource.size() == 0) {
mStopHandler = true;
}
if (!mStopHandler) {
View v = myRecyclerView.findViewHolderForAdapterPosition(0).itemView;
deleteItem(v, 0);
} else {
handler.removeCallbacksAndMessages(null);
}
handler.postDelayed(this, 250);
}
};
requireActivity().runOnUiThread(runnable);
}
同样重要的是处理清单,活动部分中的配置更改,就像清除回收者视图列表时更改配置,将引发异常
Also it's important to handle configuration change in manifest, activity section, as if the configuration changes while clearing your recycler view list, an exception will be raised
<activity
android:name=".activities.MainActivity"
android:configChanges="orientation|screenSize|keyboard"
android:label="@string/app_name">
...
</activity>
这篇关于Android单击按钮即可删除所有带有动画的recyclerview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!