【问题标题】:Android RecyclerView Adapter notifyItemRemoved cause last element in the list to blink when animateAndroid RecyclerView Adapter notifyItemRemoved 导致列表中的最后一个元素在动画时闪烁
【发布时间】:2017-04-06 05:45:26
【问题描述】:

在适配器上调用 notifyItemRemoved(position) 时遇到问题。每当我提供的位置不是最后一个元素,并且当删除动画发生时,最后一个元素总是会在向上移动之前先“闪烁/闪烁”,如下面的屏幕截图(链接)所示。

这是我的代码的一个小sn-p:

MainActivity.java

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ...

        RecylerView recyclerView = (RecyclerView) findViewById(R.id.list_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(new ItemAdapter());
    }

ItemAdapter.java

 public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // mItems has a list of hard-coded elements
                mItems.remove(holder.getAdapterPosition());
                notifyItemRemoved(holder.getAdapterPosition());
            }
        });
    }

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.myapp.myapplication.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical">
    </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/btn_click"
        android:text="Delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

有没有人经历过这个问题以及如何解决这个问题?

提前致谢。

.gif of the issue

【问题讨论】:

  • 可以发一下布局文件或者RecyclerView吗?
  • @azizbekian 抱歉,我很忙。是的,已添加 xml。我没有添加activity_main.xml,因为它是在Android Studio中创建新项目时的默认xml

标签: android android-layout listview


【解决方案1】:

尝试将 Recycler 视图的高度更改为 match_parent

【讨论】:

    【解决方案2】:

    如果你不想要任何动画,你可以添加这一行

    ((DefaultItemAnimator) recyclerViewObject.getItemAnimator()).setSupportsChangeAnimations(false);
    

    【讨论】:

    • 感谢您的提议,但我想要动画。 :)
    • 我给出的解决方案是只删除项目添加和删除的动画。在 recyclerview 中添加和删除项目的默认动画是淡入淡出,这就是您看到闪烁的原因。你可以在这里了解更多。 frogermcs.github.io/…
    • 我试过了,但我仍然得到相同的结果...顺便说一句很好的参考链接
    【解决方案3】:

    将此添加到您的代码中。

    recyclerView.getItemAnimator().setChangeDuration(0);
    

    【讨论】:

    • 嗨@Faa 我试过了,但没有用。问题依然存在:(
    猜你喜欢
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2011-02-26
    • 2015-01-31
    • 1970-01-01
    • 2014-05-31
    • 2010-10-28
    相关资源
    最近更新 更多