Firebase UI移除populateViewHolder后,如何填充RecyclerView的ViewHolder?
Fixing the Deprecated
populateViewHolder in FirebaseRecyclerAdapter for Your Chat App Hey there! I feel your pain—spending a whole night debugging adapter issues is the worst. The populateViewHolder method you're trying to use was deprecated in newer versions of FirebaseUI for RecyclerView. Let's refactor your code to use the updated API, which relies on FirebaseRecyclerOptions and onBindViewHolder instead.
Key Changes Explained
- We'll first create
FirebaseRecyclerOptionsto configure our query and model class (this replaces the old constructor parameters forFirebaseRecyclerAdapter). - Instead of
populateViewHolder, we'll overrideonBindViewHolderto bind our data to the view holder. - We need to explicitly call
startListening()andstopListening()in the fragment's lifecycle methods to manage the adapter's data sync and avoid memory leaks.
Refactored Code for FriendsFragment
Here's your updated fragment code with all the necessary fixes:
public class FriendsFragment extends Fragment { private RecyclerView mFriendsList; private DatabaseReference mFriendsDatabase; private DatabaseReference mUsersDatabase; private FirebaseAuth mAuth; private String mCurrent_user_id; private View mMainView; private FirebaseRecyclerAdapter<Friends, FriendsViewHolder> friendsRecyclerViewAdapter; public FriendsFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mMainView = inflater.inflate(R.layout.fragment_friends, container, false); mFriendsList = mMainView.findViewById(R.id.friends_list); mAuth = FirebaseAuth.getInstance(); mCurrent_user_id = mAuth.getCurrentUser().getUid(); mFriendsDatabase = FirebaseDatabase.getInstance().getReference().child("Friends").child(mCurrent_user_id); mFriendsDatabase.keepSynced(true); mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users"); mUsersDatabase.keepSynced(true); mFriendsList.setHasFixedSize(true); mFriendsList.setLayoutManager(new LinearLayoutManager(getContext())); // Configure FirebaseRecyclerOptions with our query and model FirebaseRecyclerOptions<Friends> options = new FirebaseRecyclerOptions.Builder<Friends>() .setQuery(mFriendsDatabase, Friends.class) .build(); // Initialize adapter with the new options friendsRecyclerViewAdapter = new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>(options) { @Override protected void onBindViewHolder(@NonNull final FriendsViewHolder friendsViewHolder, int position, @NonNull Friends friends) { // This replaces the old populateViewHolder method friendsViewHolder.setDate(friends.getDate()); final String list_user_id = getRef(position).getKey(); mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { final String userName = dataSnapshot.child("name").getValue().toString(); String userThumb = dataSnapshot.child("thumb_image").getValue().toString(); if(dataSnapshot.hasChild("online")) { String userOnline = dataSnapshot.child("online").getValue().toString(); friendsViewHolder.setUserOnline(userOnline); } friendsViewHolder.setName(userName); friendsViewHolder.setUserImage(userThumb, getContext()); friendsViewHolder.mView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CharSequence options[] = new CharSequence[]{"Open Profile", "Send message"}; final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle("Select Options"); builder.setItems(options, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { if(i == 0){ Intent profileIntent = new Intent(getContext(), ProfileActivity.class); profileIntent.putExtra("user_id", list_user_id); startActivity(profileIntent); } if(i == 1){ Intent chatIntent = new Intent(getContext(), ChatActivity.class); chatIntent.putExtra("user_id", list_user_id); chatIntent.putExtra("user_name", userName); startActivity(chatIntent); } } }); builder.show(); } }); } @Override public void onCancelled(DatabaseError databaseError) { } }); } @NonNull @Override public FriendsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // Inflate the single user layout and return our view holder View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.users_single_layout, parent, false); return new FriendsViewHolder(view); } }; mFriendsList.setAdapter(friendsRecyclerViewAdapter); return mMainView; } @Override public void onStart() { super.onStart(); // Start listening for realtime data updates friendsRecyclerViewAdapter.startListening(); } @Override public void onStop() { super.onStop(); // Stop listening to prevent memory leaks when fragment is inactive friendsRecyclerViewAdapter.stopListening(); } public static class FriendsViewHolder extends RecyclerView.ViewHolder { View mView; public FriendsViewHolder(View itemView) { super(itemView); mView = itemView; } public void setDate(String date){ TextView userStatusView = mView.findViewById(R.id.user_single_status); userStatusView.setText(date); } public void setName(String name){ TextView userNameView = mView.findViewById(R.id.user_single_name); userNameView.setText(name); } public void setUserImage(String thumb_image, Context ctx){ CircleImageView userImageView = mView.findViewById(R.id.user_single_image); Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(userImageView); } public void setUserOnline(String online_status) { ImageView userOnlineView = mView.findViewById(R.id.user_single_online_icon); if(online_status.equals("true")){ userOnlineView.setVisibility(View.VISIBLE); } else { userOnlineView.setVisibility(View.INVISIBLE); } } } }
What Changed?
- Added
FirebaseRecyclerOptions: This is the new way to define your query and model class for the adapter, replacing the old constructor arguments. - Replaced
populateViewHolderwithonBindViewHolder: All your data binding logic moves here, usingpositionto fetch the correct reference key. - Added
onCreateViewHolder: This method handles inflating the list item layout and creating the view holder (previously handled automatically by the deprecated adapter). - Lifecycle Management: We added
startListening()andstopListening()calls to sync data only when the fragment is active, which prevents unnecessary resource usage and memory leaks.
This should get your friends list working smoothly with the latest FirebaseUI API!
内容的提问来源于stack exchange,提问作者Rohan Suri
相关产品推荐
相关产品推荐

