当我使用它时,它会删除带有动画的一个元素
{
notificationItems.remove(0);
adapterForNotification.notifyItemRemoved(0);
adapterForNotification.notifyItemRangeRemoved(0,count-1);
}
当我使用它时,它会删除所有没有动画的元素
count = adapter.getItemCount();
for(int i = 0 ; i < count; ++i){
notificationItems.remove(0);
adapterForNotification.notifyItemRemoved(0);
adapterForNotification.notifyItemRangeRemoved(0,count-1)
}
答案 0 :(得分:0)
您不应该同时使用notifyItemRemoved()
和notifyItemRangeRemoved()
。一次只能使用一个。
如果要删除一项:
notificationItems.remove(index);
adapterForNotification.notifyItemRemoved(index);
如果要删除所有项目:
int origCount = notificationItems.size();
notificationItems.clear();
adapterForNotification.notifyItemRangeRemoved(0, origCount - 1);
如果要删除一系列项目:
notificationItems.subList(startIndex, endIndex).clear();
adapterForNotification.notifyItemRangeRemoved(startIndex, endIndex);
编辑:
如果您要一个一个地删除每个项目并显示每个项目的删除动画,请尝试以下操作:
for (int i = 0; i < notificationItems.size(); i++) {
notificationItems.remove(i);
adapterForNotification.notifyItemRemoved(i);
}
答案 1 :(得分:0)
count = adapter.getItemCount();
for (int i = 0 ; i < count; ++i){
notificationItems.remove(0);
}
adapterForNotification.notifyItemRangeRemoved(0, count-1)
答案 2 :(得分:0)
据我了解,您可以删除项目,但删除时需要添加某种动画;请看看design guidelines
,如果有人在这里停留,则会再次在此处发布
它已经很旧了,但希望它对其他人有所帮助,因为它尚未得到答复;我通过模拟一个项目的一次滑动动画来一次删除一个项目,并在删除下一个项目之前发布了一个延迟,依此类推直到RecyclerView
<的最后一个项目。 / p>
第1步:
在拥有“清除所有”按钮和RecyclerView
实例的活动中:创建一种删除单个项目的方法
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
列表项>>在按钮单击回调中调用它。
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);
}
同样重要的是,在清单,活动部分中处理配置更改,就好像在清除回收者视图列表时配置更改一样,也会引发异常
<activity
android:name=".activities.MainActivity"
android:configChanges="orientation|screenSize|keyboard"
android:label="@string/app_name"
...
</activity>
答案 3 :(得分:0)
为此,我直接从recyclerview的适配器访问列表项的视图,对其进行动画处理,并在播放最后一个动画后通知适配器。
fun deleteMultipleAnimated(){
val oldSize = myList.size
myList.removeAt(3)
myList.removeAt(4)
val newSize = myList.size
for(i in newSize until oldSize){
val listItemView = myRecycler.findViewHolderForAdapterPosition(i) as MyAdapater.MyViewHolder
if(i == oldSize-1){
//only the last animation notifies the adapter
listItemView.myView.animate().scaleX(0f).setDuration(250).withEndAction{
myAdapter.notifyItemRangeRemoved(newSize, oldSize)}
}else{
//every other view is animated without an end action
listItemView.myView.animate().scaleX(0f).duration = 250
}
}
}