回收者视图未根据实时更新进行更新

时间:2019-01-11 10:19:13

标签: android android-recyclerview google-cloud-firestore real-time sectionedrecyclerviewadapter

我正在尝试在用户发送和删除好友请求时更新回收者视图。.i我正在使用Firebase Firestore ..当一个用户发送好友请求请求时,它会显示在回收者视图中,而当请求被取消时,用户不会被删除从列表中我正在使用以下代码,

case ADDED:
                                    Log.d(TAG, "onEvent: " + dc.getDocument().toString());
                                    if (state.equals("recieved")) {

                                        mDb.collection("users").document(userId)
                                                .get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                            @Override
                                            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                                Log.d(TAG, "onComplete: CheckingifTAskComplete");
                                                if (task.isSuccessful()) {
                                                    Log.d(TAG, "onComplete: tassk: succes :" + task.getResult());

                                                    DocumentSnapshot snapshot = task.getResult();
                                                    Users users = new Users(snapshot.getString("id"),
                                                            snapshot.getString("display_name"),
                                                            snapshot.getString("status"),
                                                            snapshot.getString("profile_image"),
                                                            snapshot.getString("thumb_image"));

                                                    Log.d(TAG, "onComplete: useradded" + users.toString());

                                                    mUSerRequestList.add(users);
                                                    adapter.notifyItemInserted(adapter.getItemCount()-1);
                                                }
                                            }
                                        }).addOnFailureListener(new OnFailureListener() {
                                            @Override
                                            public void onFailure(@NonNull Exception e) {
                                                Log.d(TAG, "onFailure: Failed");
                                            }
                                        });

删除时:

case REMOVED:
                                    Log.d("TAG", "Removed Msg: " + dc.getDocument());

                                    mDb.collection("users").document(userId)
                                            .get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                        @Override
                                        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                            Log.d(TAG, "onComplete: CheckingifTAskComplete");
                                            if (task.isSuccessful()) {
                                                Log.d(TAG, "onComplete: tassk: succes :" + task.getResult());

                                                DocumentSnapshot snapshot = task.getResult();
                                                Users users = new Users(snapshot.getString("id"),
                                                        snapshot.getString("display_name"),
                                                        snapshot.getString("status"),
                                                        snapshot.getString("profile_image"),
                                                        snapshot.getString("thumb_image"));

                                                for (Users removeUser: mUSerRequestList) {

                                                    if (removeUser.getId().equals(users.getId())){
                                                        mUSerRequestList.remove(users);
                                                        adapter.notifyItemRemoved(mUSerRequestList.indexOf(users.getId()));
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    });
                                    break;

我的回收站视图适配器:

public class RequestsRecyclerViewAdapter extends RecyclerView.Adapter<RequestViewHolder> {

private static final String TAG = "RequestsRecyclerViewAda";
ArrayList<Users> mList;
private Context mainActivityContext;
static RequestViewHolder ho;


public RequestsRecyclerViewAdapter(Context mainActivityContext, ArrayList<Users> mList) {
    this.mList = mList;
    this.mainActivityContext = mainActivityContext;
}

@NonNull
@Override
public RequestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
    Log.d(TAG, "onCreateViewHolder: inflatingUserActivity");
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.snippet_user_request_single, parent, false);

    Log.d(TAG, "onCreateViewHolder: CallingUSersViewHolder");
    return new RequestViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final RequestViewHolder holder, final int position) {

    ho = holder;
    Log.d(TAG, "onBindViewHolder: RequestUSerGot: " + mList.get(position).toString());

    Log.d(TAG, "onBindViewHolder: " + holder.mName);

    holder.mName.setText(mList.get(position).getDisplay_name());
    Picasso.get()
            .load(mList.get(position).getThumb_image())
            .networkPolicy(NetworkPolicy.OFFLINE)
            .placeholder(R.drawable.ic_launcher_foreground)
            .into(holder.mProfileImage, new Callback() {
                @Override
                public void onSuccess() {

                }

                @Override
                public void onError(Exception e) {
                    Picasso.get()
                            .load(mList.get(position).getThumb_image())
                            .placeholder(R.drawable.ic_launcher_foreground)
                            .into(holder.mProfileImage);
                }
            });
    Log.d(TAG, "onBindViewHolder: picassoLoaded");



    addonClickListners(holder);

}

private void addonClickListners(RequestViewHolder holder) {
    holder.mAccept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mainActivityContext, "Added", Toast.LENGTH_SHORT).show();
        }
    });

    holder.mCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mainActivityContext, "Canceld", Toast.LENGTH_SHORT).show();
        }
    });

}


@Override
public int getItemCount() {
    return mList.size();
}

}

观看者:

public class RequestViewHolder extends RecyclerView.ViewHolder {

public CircleImageView mProfileImage;
public TextView mName;
public ImageButton mAccept, mCancel;

public RequestViewHolder(@NonNull View itemView) {
    super(itemView);

    mProfileImage = itemView.findViewById(R.id.requestFPRofileImage);
    mName = itemView.findViewById(R.id.requestFName);
    mAccept = itemView.findViewById(R.id.requestFAccept);
    mCancel = itemView.findViewById(R.id.requestFCancel);
}

}

1 个答案:

答案 0 :(得分:0)

在notifyItemRemoved和notifyIteminserted之后使用adapter.notifyDataSetChanged(),希望它对您有用。