android - RecyclerView:在转换片段期间调用 notifyDataSetChanged == 冻结
问题描述
我没有找到类似的问题,所以我问。我有:FirstFragment、SecondFragment 和过渡:
<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 上,我模拟在转换期间获取数据并显示数据:
类SecondFragment:片段(){
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>
解决方案
如果您想等待动画完成,您将覆盖onCreateAnimator#onAnimationEnd
您的Fragment
然后调用一个方法来“初始化”您的视图,如下所示:
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
}
但是这一行:
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
推荐阅读
- swift - iOS 13 透明工具栏
- python - 为什么 PyTrends/Pandas 程序会抛出错误代码:raise ValueError("No objects to concatenate") ValueError: No objects to concatenate?
- unity3d - 实例化不能与 navmesh 统一工作
- python - unittest python模块找不到模块
- python - 如何将列表转换为字典并连接值?
- sql - Google BigQuery 维度 SQL 多个网格
- database - 如何查看数据框中的所有数据?通过 NBA_API
- python-requests - 使用请求下载 excel 报告时出现间歇性 nullpointerexception 错误
- javascript - 使用泛型扩展模块
- r-markdown - 重新安装故障硬盘后,RMarkdown 图形现在大小不同