[英]notifyItemInserted() is having same effect on list as notifyDataSetChanged() when I only want last item in list updated
The problem that I am having is that when a user adds a comment the entire list refreshes because I have notifyDataSetChanged();
我遇到的问题是,当用户添加评论时,整个列表都会刷新,因为我有notifyDataSetChanged();
set on the CommentAdapter
.在CommentAdapter
上设置。 Everything jumps, and refreshes and I want it to be smoother, so I decided to use notifyItemInserted();
一切都在跳跃,刷新,我希望它更流畅,所以我决定使用notifyItemInserted();
instead of notifyDataSetChanged();
而不是notifyDataSetChanged();
, but it isn't doing anything different. ,但它没有做任何不同的事情。
notifyItemInserted();
should only update the last item or the newest item added to the list, so why is everything being refreshed/updated...?应该只更新最后一项或添加到列表中的最新项,那么为什么所有内容都被刷新/更新......?
Can someone tell me how to fix this?有人可以告诉我如何解决这个问题吗? Only want last item added to list to be "added"/"updated"/whatever, not the entire list because if many people start commenting everything is always reloading...只希望添加到列表中的最后一项被“添加”/“更新”/无论如何,而不是整个列表,因为如果很多人开始评论一切总是重新加载......
In my readComments();
在我的readComments();
what I have now is mCommentAdapter.notifyItemInserted(mCommentList.size() - 1);
我现在拥有的是mCommentAdapter.notifyItemInserted(mCommentList.size() - 1);
, and what I had before was mCommentAdapter.notifyDataSetChanged();
,而我之前拥有的是mCommentAdapter.notifyDataSetChanged();
, but they are having the same effect. ,但它们具有相同的效果。 How can I fix this?我怎样才能解决这个问题?
CommentsActivity评论活动
public class CommentsActivity extends AppCompatActivity {
private CommentAdapter mCommentAdapter;
private List<Comment> mCommentList;
private RecyclerView mRecyclerView;
EditText mAddComment;
ImageView mImageProfile;
TextView mPost;
String mPostId;
String mPublisherId;
String mNotificationId;
FirebaseUser mFirebaseUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
mCommentList = new ArrayList<>();
mCommentAdapter = new CommentAdapter(this, mCommentList, mPostId);
mRecyclerView.setAdapter(mCommentAdapter);
mAddComment = findViewById(R.id.add_comment);
mImageProfile = findViewById(R.id.image_profile);
mPost = findViewById(R.id.post_comment);
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
mPost.setOnClickListener(v -> {
if (mAddComment.getText().toString().equals("")) {
Toast.makeText(CommentsActivity.this, "Can't send empty comments", Toast.LENGTH_SHORT).show();
} else {
addCommentNotification(mPublisherId, mPostId);
}
});
getImage();
readComments();
}
private void getImage() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(mFirebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
if (user != null)
Glide.with(getApplicationContext()).load(user.getImageurl()).into(mImageProfile);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void readComments() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments").child(mPostId);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mCommentList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Comment comment = snapshot.getValue(Comment.class);
mCommentList.add(comment);
}
mCommentAdapter = new CommentAdapter(CommentsActivity.this, mCommentList, mPostId);
mRecyclerView.setAdapter(mCommentAdapter);
mCommentAdapter.notifyItemInserted(mCommentList.size() - 1);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
I am guessing you should be using a ChildEventListener
:我猜你应该使用ChildEventListener
:
Your readComments()
must be like this:您的readComments()
必须是这样的:
private void readComments() {
//your reference
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Comments").child(mPostId);
//the child listener
ChildEventListener listener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
// A new comment has been added
Comment comment = dataSnapshot.getValue(Comment.class);
mCommentList.add(comment);
//Notify adapter
mCommentAdapter.notifyItemInserted(mCommentList.size() - 1);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
//attach the listener
ref.addChildEventListener(listener);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.