繁体   English   中英

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

[英]notifyItemInserted() is having same effect on list as notifyDataSetChanged() when I only want last item in list updated

我遇到的问题是,当用户添加评论时,整个列表都会刷新,因为我有notifyDataSetChanged(); CommentAdapter上设置。 一切都在跳跃,刷新,我希望它更流畅,所以我决定使用notifyItemInserted(); 而不是notifyDataSetChanged(); ,但它没有做任何不同的事情。

notifyItemInserted(); 应该只更新最后一项或添加到列表中的最新项,那么为什么所有内容都被刷新/更新......?

有人可以告诉我如何解决这个问题吗? 只希望添加到列表中的最后一项被“添加”/“更新”/无论如何,而不是整个列表,因为如果很多人开始评论一切总是重新加载......

在我的readComments(); 我现在拥有的是mCommentAdapter.notifyItemInserted(mCommentList.size() - 1); ,而我之前拥有的是mCommentAdapter.notifyDataSetChanged(); ,但它们具有相同的效果。 我怎样才能解决这个问题?

评论活动

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) {

            }
        });
    }
}

我猜你应该使用ChildEventListener

您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM