我正在开发一个Android应用程序,该程序可从RecyclerView中的Firebase数据库加载数据。而且我注意到当网络速度很低时,回收站视图不会显示任何内容,并且会花费一些时间来显示数据。因此,我想在RecyclerView中使用加载进度栏,直到RecyclerView准备显示数据为止。但是我不知道该怎么做,需要帮助。
答案 0 :(得分:0)
您没有提供任何代码,所以我根据我的方法回答了您的问题:
XML(我们有一个带有RecyclerView和Progressbar的ConstraintLayout)
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp" />
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在您的Java代码(MainActivity)中:
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.viewTreeObserver
.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
progressbar.visibility = View.GONE
recyclerView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
recyclerView.adapter = RecyclerViewAdapter(yourItems)