【问题标题】:Retrive user uid details in onBindViewHolder RecyclerView Firebase Adapter | FirebaseUI | Android ,Java在 onBindViewHolder RecyclerView Firebase 适配器中检索用户 uid 详细信息 |火力基地 |安卓,Java
【发布时间】:2020-04-28 11:44:00
【问题描述】:

在这里,当我快速滚动时,我遇到了“持有人”的问题,持有人位置得到更新(因为回收行为),过了一会儿 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

【问题讨论】:

    标签: android android-recyclerview firebaseui


    【解决方案1】:

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

    拨打您的adapter

    adapter.setHasStableIds(true);
    

    在您的adapter 中覆盖onBindViewHolder() 下的2 个方法:

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

    【讨论】:

    • 它的工作!但是物品没有回收,内存优化可以吗??
    • 你删除回收站了吗。
    • 你的问题很常见,人们通常按照我告诉你的方式解决它。我认为您对此无能为力。
    • 您必须阅读有关 recyclerview 适配器功能的更多信息,如果这解决了您的问题,请接受答案,以便其他有相同问题的人知道去哪里查找。
    猜你喜欢
    • 2021-02-03
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2016-04-08
    相关资源
    最近更新 更多