如何添加 recyclerview 项目删除动画

Els*_*sli 9 android removeall android-animation android-recyclerview

当我使用它时,它会删除一个带有动画的元素

{
    notificationItems.remove(0);
    adapterForNotification.notifyItemRemoved(0);                        
    adapterForNotification.notifyItemRangeRemoved(0,count-1);
}
Run Code Online (Sandbox Code Playgroud)

但是,当我使用它时,它会删除所有没有动画的元素

count = adapter.getItemCount();
for(int i = 0 ; i < count; ++i){
    notificationItems.remove(0);
    adapterForNotification.notifyItemRemoved(0);
    adapterForNotification.notifyItemRangeRemoved(0,count-1)
}
Run Code Online (Sandbox Code Playgroud)

Zai*_*ain 10

据我所知,您可以删除项目,但您需要在删除时添加某种动画;请看这里

如果有人来这里,会再次在这里发帖

它很旧,但希望这对其他人有所帮助,因为它还没有得到回答;我通过在此项目上模拟滑动动画一次删除一个项目来完成它,并在删除下一个项目之前发布延迟,依此类推到最后一个项目的方式RecyclerView

第 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());
}
Run Code Online (Sandbox Code Playgroud)

第 2 步:

创建将删除所有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);
}
Run Code Online (Sandbox Code Playgroud)

同样重要的是处理清单,活动部分中的配置更改,就像在清除回收站视图列表时配置更改一样,将引发异常

<activity
    android:name=".activities.MainActivity"
    android:configChanges="orientation|screenSize|keyboard"
    android:label="@string/app_name"
    ...
</activity>
Run Code Online (Sandbox Code Playgroud)


The*_*rer 3

您不应该同时使用notifyItemRemoved()notifyItemRangeRemoved()。一次只能使用一个。

如果您想删除一项:

notificationItems.remove(index);
adapterForNotification.notifyItemRemoved(index);
Run Code Online (Sandbox Code Playgroud)

如果您想删除所有项目:

int origCount = notificationItems.size();
notificationItems.clear();
adapterForNotification.notifyItemRangeRemoved(0, origCount - 1);
Run Code Online (Sandbox Code Playgroud)

如果您想删除一系列项目:

notificationItems.subList(startIndex, endIndex).clear();
adapterForNotification.notifyItemRangeRemoved(startIndex, endIndex);
Run Code Online (Sandbox Code Playgroud)

编辑:

如果您想逐个删除每个项目并显示每个项目的删除动画,请尝试以下操作:

for (int i = 0; i < notificationItems.size(); i++) {
    notificationItems.remove(i);
    adapterForNotification.notifyItemRemoved(i);
}
Run Code Online (Sandbox Code Playgroud)