我正在开发一个安卓应用程序,在RecyclerView中加载数据从Firebase数据库。ai注意到,当网络速度很低时,回收器视图不会显示任何内容,并且需要时间来显示数据。所以我想使用RecyclerView的加载进度条,直到RecyclerView准备好显示data.But,我不知道该怎么做,需要帮助。
发布于 2020-05-05 19:20:19
您没有提供任何代码,所以我根据我的方法回答您的问题:
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)https://stackoverflow.com/questions/61610021
复制相似问题