显示进度条,直到数据被放入适配器



我在一个项目中与FirebaseRecyclerView合作。这里我从firebase获得一些数据到适配器,并在RecyclerView中显示它们。它工作得很好,但是从firebase获取数据需要一些时间。所以我使用了一个进度条,它是可见的,直到数据从firebase检索。但是这里有一个问题,当数据在适配器中设置它们的位置时,它是不可见的。

这是一个视频在这个

代码在这里

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_messenger)
progress_circular_chat.visibility = VISIBLE          // here it goes visible
nested_sv_chat.visibility = GONE
retrieveChatList()
}

private fun retrieveChatList() {
usersChatList = ArrayList()
val userRef = dbRef.child("ChatList").child(currentUserID)
userRef.addValueEventListener(object : ValueEventListener
{
override fun onCancelled(error: DatabaseError) {
}
override fun onDataChange(snapshot: DataSnapshot)
{
(usersChatList as ArrayList<String>).clear()
if (snapshot.exists()){
for (dataSnapshot in snapshot.children){
val userUid = dataSnapshot.key
if (userUid != null) {
(usersChatList as ArrayList<String>).add(userUid)
}
}
readChatList()
}
retrieveGroupChatList()
}
})
}

private fun readChatList() {
mUsers = ArrayList()
val userRef = FirebaseFirestore.getInstance().collection("Users")
userRef.get()
.addOnSuccessListener { queryDocumentSnapshots ->
mUsers?.clear()
for (documentSnapshot in queryDocumentSnapshots) {
val user = documentSnapshot.toObject(User::class.java)
for (id in usersChatList!!){
if (user.getUid() == id){
(mUsers as ArrayList<User>).add(user)
}
}
}
chatListAdapter?.notifyDataSetChanged()
chatListAdapter = ChatListAdapter(this, (mUsers as ArrayList<User>), true)
recyclerViewChatList.adapter = chatListAdapter

}.addOnFailureListener { e -> }
}

private fun retrieveGroupChatList() {
//same code as readChatList for group chats
progress_circular_chat.visibility = GONE  // here it goes invisible
nested_sv_chat.visibility = VISIBLE
}

下面是xml代码

<ProgressBar
android:id="@+id/progress_circular_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:indeterminate="true"
android:visibility="gone"
android:progressTint="@color/button_color"/>

<androidx.core.widget.NestedScrollView
android:id="@+id/nested_sv_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fillViewport="true"
android:overScrollMode="never">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_chatList_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:nestedScrollingEnabled="false"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_chatList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:nestedScrollingEnabled="false"/>
</androidx.core.widget.NestedScrollView>

设置ProgressBar可见性为VISIBLE时,同时设置RecyclerView可见性为GONE。然后,当加载完成时,只需将ProgressBar的可见性设置为GONE,RecyclerView的可见性设置为VISIBLE

你这样做的方式,即使我没有看到你的XML代码,这是你只是显示你的进度条,但它是不可见的,因为你的RecyclerView。当数据被加载,有一个轻微的状态刷新从RecyclerView允许进度条可见一秒钟,但它又消失了。

编辑:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_messenger)
progress_circular_chat.visibility = View.VISIBLE
recyclerViewChatList.visibility = View.GONE //THIS LINE IS ADDED !!!!!!!!!!!!!!!!!!
readChatList()
}    
private fun readChatList() {
mUsers = ArrayList()
val userRef = FirebaseFirestore.getInstance().collection("Users")
userRef.get()
.addOnSuccessListener { queryDocumentSnapshots ->
mUsers?.clear()
for (documentSnapshot in queryDocumentSnapshots) {
val user = documentSnapshot.toObject(User::class.java)
for (id in usersChatList!!){
if (user.getUid() == id){
(mUsers as ArrayList<User>).add(user)
}
}
}
chatListAdapter?.notifyDataSetChanged()
chatListAdapter = ChatListAdapter(this, (mUsers as ArrayList<User>), true)
recyclerViewChatList.adapter = chatListAdapter
progress_circular_chat.visibility = View.GONE 
recyclerViewChatList.visibility = View.VISIBLE //THIS LINE IS ADDED !!!!!!!!!!!!!!!!!!!!!!
}.addOnFailureListener { e ->
progress_circular_chat.visibility = View.GONE 
}
}

相关内容

最新更新