RecyclerView: 在过渡片段期间调用 notifyDataSetChanged 会导致卡顿。

3

我没有找到类似的问题,所以我提问。 我有: FirstFragment、SecondFragment和transition:

<navigation 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:id="@+id/nav_graph"
            app:startDestination="@id/FirstFragment">

    <fragment
        android:id="@+id/FirstFragment"
        android:name="testrenderinglistontransition.FirstFragment"
        android:label="@string/first_fragment_label"
        tools:layout="@layout/fragment_first">

        <action
            app:enterAnim="@anim/right_in"
            android:id="@+id/action_FirstFragment_to_SecondFragment"
            app:destination="@id/SecondFragment"/>
    </fragment>
    <fragment
        android:id="@+id/SecondFragment"
        android:name="testrenderinglistontransition.SecondFragment"
        android:label="@string/second_fragment_label"
        tools:layout="@layout/fragment_second">

        <action
            app:enterAnim="@anim/right_in"
            android:id="@+id/action_SecondFragment_to_FirstFragment"
            app:destination="@id/FirstFragment"/>
    </fragment>
</navigation>

在SecondFragment中,我模拟了数据的获取和显示过程:

class SecondFragment : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_second, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val recyclerview : RecyclerView = view.findViewById(R.id.recyclerview)
    val progress : ProgressBar = view.findViewById(R.id.progress)
    val adapter = CustomAdapter(arrayOf())
    progress.visibility =View.VISIBLE
    recyclerview.adapter = adapter
    recyclerview.layoutManager = LinearLayoutManager(context).apply {
        orientation = LinearLayoutManager.VERTICAL
    }

    val data = getData()

    Handler().postDelayed(Runnable {
        progress.visibility =View.GONE
        adapter.dataSet = data
        adapter.notifyDataSetChanged()
    },250) // transition time is 300 so we should call notifyDataSetChanged before ending of transition

}

private fun getData() : Array<String>{
    val list = mutableListOf<String>()
    for(i in 0 .. 1000){
        list.add(UUID.randomUUID().toString())
    }
    return list.toTypedArray()
}

}

我有什么:

你有任何想法如何修复它吗?我认为我应该等待过渡结束。

你可以看一下适配器和布局,但它们真的很简单:

class CustomAdapter(var dataSet: Array<String>) :
    RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    /**
     * Provide a reference to the type of views that you are using
     * (custom ViewHolder).
     */
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val textView: TextView

        init {
            // Define click listener for the ViewHolder's View.
            textView = view.findViewById(R.id.textView)
        }
    }

    // Create new views (invoked by the layout manager)
    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        // Create a new view, which defines the UI of the list item
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.text_row_item, viewGroup, false)

        return ViewHolder(view)
    }

    // Replace the contents of a view (invoked by the layout manager)
    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {

        // Get element from your dataset at this position and replace the
        // contents of the view with that element
        viewHolder.textView.text = dataSet[position]
    }

    // Return the size of your dataset (invoked by the layout manager)
    override fun getItemCount() = dataSet.size

}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/image"
        android:src="@android:drawable/alert_dark_frame"
        android:layout_width="30dp"
        android:layout_height="30dp"/>

    <TextView
        android:layout_alignLeft="@+id/image"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
2个回答

2
如果您想要等待动画完成,您需要覆盖Fragment的onCreateAnimator#onAnimationEnd方法,并调用一个方法来“初始化”您的视图,如下所示:
override fun onCreateAnimator(transit: Int, enter: Boolean, nextAnim: Int): Animator? {
    var animator = super.onCreateAnimator(transit, enter, nextAnim)
    
    if (enter) {
        animator?.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                super.onAnimationEnd(animation)
                initView()
            }
        })
    }

    return animator        
}

private fun initView() {
    // TODO create your recycler views etc here
}

However this line:

val data = getData() 

应该从单独的线程调用,因为这是导致UI冻结的真正原因(您正在UI线程上执行大量工作),可以使用以下方式:
fun loadData() = CoroutineScope(Dispatchers.Main).launch {
        // TODO show progress bar here
        val task = async(Dispatchers.IO) {
            getData()
        }
        data = task.await()
        adapter.dataSet = data
        adapter.notifyDataSetChanged()
        // TODO hide progress bar here
    }

现在可以调用loadData()来代替所有这些行:
 val data = getData()

    Handler().postDelayed(Runnable {
        progress.visibility =View.GONE
        adapter.dataSet = data
        adapter.notifyDataSetChanged()
    },250) // transition time is 300 so we should call notifyDataSetChanged before ending of transition

在我的示例中,getData() 只是创建数组,因此非常快。但在实际情况下,我们必须使用异步代码,你是正确的。 - Puzirki
它并不是非常快,这就是为什么你可以看到 UI 上的暂停。虽然只有几毫秒,但仍会导致 UI 冻结。 - David Kroukamp
我测试过了 - 异步加载数据(或者从之前准备好的静态字段获取数据或其他方式)并不能解决问题。但无论如何,非常感谢你的建议。 - Puzirki

0

David Kroukamp的回答对我没有帮助,因为super.onCreateAnimator(transit, enter, nextAnim)返回null。

但是我有另一个解决方案:

Handler().postDelayed(Runnable {
        progress.visibility =View.GONE
        adapter.dataSet = data
        adapter.notifyDataSetChanged()
    },300) // transition time is 300 so we will wait end of transition.

有一个缺点:

如果数据加载需要200毫秒,那么我们将在200 + 300毫秒后绘制数据,因此200毫秒我们没有任何理由等待。但是我们可以测量加载数据的时间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接