在 onBindViewHolder RecyclerView Firebase Adapter 中检索用户 uid 详细信息 |用户界面 | Android ,Java

Retrive user uid details in onBindViewHolder RecyclerView Firebase Adapter | FirebaseUI | Android ,Java

这里我遇到了 'holder' 的问题,因为我快速滚动,holder 位置得到更新(因为回收行为)并且在一段时间后 onSuccess 被调用(因为获取数据需要一些时间) 直到那时持有人的位置已经改变并且 holder.mUserFullName.setText(user.getName()) 应用于错误的持有人位置。 这只会在我快速滚动时发生,如果我禁用回收然后它解决了问题但我想要回收功能。

@Override
        protected void onBindViewHolder(@NonNull PostVH holder, int position, @NonNull Post post) {

            MyFirestoreDbRefs.getAllUsersCollection().document(post.getByUid())
                    .get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    User user=documentSnapshot.toObject(User.class);

                    /*here i face a problem with 'holder' as i fast scroll,holder position gets updated
                    * (because of recycling behaviour) and after a while onSuccess gets called(as it takes some time for fetching data) 
                    * until then the holder position already changed and holder.mUserFullName.setText(user.getName()) applied to wrong 
                    * holder position */

                    holder.mUserFullName.setText(user.getName());
                }
            });

        }

Post Collection Structure in DB Users Collection Structure in DB

以下解决方案可能适用于某些情况:

在您的 adapter 上调用它:

adapter.setHasStableIds(true);

在您的 adapter 重写 2 个方法中,在 onBindViewHolder() 下:

@Override
public long getItemId(int position) {
return position;
}

@Override
public int getItemViewType(int position) {
return position;
}