您可以使用标准的RecyclerView。
例如:
持有人:
private static class CommentViewHolder extends RecyclerView.ViewHolder {
public TextView authorView;
public TextView bodyView;
public CommentViewHolder(View itemView) {
super(itemView);
authorView = (TextView) itemView.findViewById(R.id.comment_author);
bodyView = (TextView) itemView.findViewById(R.id.comment_body);
}
}
适配器:
private static class CommentAdapter extends RecyclerView.Adapter<CommentViewHolder> {
private Context mContext;
private DatabaseReference mDatabaseReference;
private ChildEventListener mChildEventListener;
private List<String> mCommentIds = new ArrayList<>();
private List<GoogleExample_Comment> mGoogleExampleComments = new ArrayList<>();
public CommentAdapter(final Context context, DatabaseReference ref) {
mContext = context;
mDatabaseReference = ref;
// Create child event listener
// [START child_event_listener_recycler]
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
// A new googleExampleComment has been added, add it to the displayed list
GoogleExample_Comment googleExampleComment = dataSnapshot.getValue(GoogleExample_Comment.class);
// [START_EXCLUDE]
// Update RecyclerView
mCommentIds.add(dataSnapshot.getKey());
mGoogleExampleComments.add(googleExampleComment);
notifyItemInserted(mGoogleExampleComments.size() - 1);
// [END_EXCLUDE]
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
// A comment has changed, use the key to determine if we are displaying this
// comment and if so displayed the changed comment.
GoogleExample_Comment newGoogleExampleComment = dataSnapshot.getValue(GoogleExample_Comment.class);
String commentKey = dataSnapshot.getKey();
// [START_EXCLUDE]
int commentIndex = mCommentIds.indexOf(commentKey);
if (commentIndex > -1) {
// Replace with the new data
mGoogleExampleComments.set(commentIndex, newGoogleExampleComment);
// Update the RecyclerView
notifyItemChanged(commentIndex);
} else {
Log.w(TAG, "onChildChanged:unknown_child:" + commentKey);
}
// [END_EXCLUDE]
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
// A comment has changed, use the key to determine if we are displaying this
// comment and if so remove it.
String commentKey = dataSnapshot.getKey();
// [START_EXCLUDE]
int commentIndex = mCommentIds.indexOf(commentKey);
if (commentIndex > -1) {
// Remove data from the list
mCommentIds.remove(commentIndex);
mGoogleExampleComments.remove(commentIndex);
// Update the RecyclerView
notifyItemRemoved(commentIndex);
} else {
Log.w(TAG, "onChildRemoved:unknown_child:" + commentKey);
}
// [END_EXCLUDE]
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
// A comment has changed position, use the key to determine if we are
// displaying this comment and if so move it.
GoogleExample_Comment movedGoogleExampleComment = dataSnapshot.getValue(GoogleExample_Comment.class);
String commentKey = dataSnapshot.getKey();
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "postComments:onCancelled", databaseError.toException());
Toast.makeText(mContext, "Failed to load comments.",
Toast.LENGTH_SHORT).show();
}
};
ref.addChildEventListener(childEventListener);
// [END child_event_listener_recycler]
// Store reference to listener so it can be removed on app stop
mChildEventListener = childEventListener;
}
@Override
public CommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.google_example_item_comment, parent, false);
return new CommentViewHolder(view);
}
@Override
public void onBindViewHolder(CommentViewHolder holder, int position) {
GoogleExample_Comment googleExampleComment = mGoogleExampleComments.get(position);
holder.authorView.setText(googleExampleComment.author);
holder.bodyView.setText(googleExampleComment.text);
}
@Override
public int getItemCount() {
return mGoogleExampleComments.size();
}
public void cleanupListener() {
if (mChildEventListener != null) {
mDatabaseReference.removeEventListener(mChildEventListener);
}
}
}
希望它可以帮助你!
酷很容易解决!谢谢! 奇怪的是,它并没有提到有关更新firebaseUI依赖项的警告,因为它在其他依赖项中发出警告。 –