简体   繁体   English

当我只想更新列表中的最后一项时,notifyItemInserted() 对列表的影响与 notifyDataSetChanged() 相同

[英]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.

相关问题 列表视图上的删除按钮只删除最后一项,而不是我要删除的特定项目 - Delete button on list view only deletes the last item and not the particular one I want it to delete 为什么当我单击任何列表项的按钮时,按钮的背景图像仅更改为最后一个列表项的按钮? - Why background images of buttons are changing for last list item's button only,when I click on any list item's button? NotifyDataSetChanged() 未显示更新的列表 - NotifyDataSetChanged() isn't showing the updated list 遍历 List 但它只显示最后一项 - Iterating through List but it only shows last item 我的Recycleview显示列表和完整列表仅间歇地显示我添加到ArrayList的最后一项 - My Recycleview display list and complete list only intermittently displays the last item I add to my ArrayList 当我单击列表中的每个项目时,我需要获取项目的内容(图像和文本) - I need when I click in every item in the list I want to get the content of item (IMAGE AND TEXT) 添加数据时notifyDataSetChanged()不更新列表视图 - notifyDataSetChanged() not updating list view when adding data 我想从列表视图中删除项目 - I want to delete an item from a list view 返回列表中的最后一项 - Return the last item in the list 当我从数组列表中添加一组标记时,它仅显示数组列表中的最后一个值? - when I add a set of markers from array list it show only the last value from array list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM