我有一个RecyclerView适配器
public class AllPostAdapter extends RecyclerView.Adapter<PostHolder> {
private AppCompatActivity mContext;
private List<Post> postList;
public AllPostAdapter(AppCompatActivity mContext, List<Post> postList){
this.mContext = mContext;
this.postList = postList;
}
// skipping other overridden methods
}
使用ChildEventListener作为
从MainActivity中的Firebase数据库中获取数据mPostChildEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
try{
if (dataSnapshot.exists() && dataSnapshot.hasChildren() && !dataSnapshot.equals(null)){
Post post = dataSnapshot.getValue(Post.class);
mPostList.add(post);
mAllPostAdapter.notifyDataSetChanged();
} else {
///
}
} catch (Exception e){
e.printStackTrace();
}
}
// skipping other overridden methods
};
mPostQuery = getQuery(); // this method only retuns ==> return mPostDatabaseReference.limitToFirst(100);
mPostQuery.addChildEventListener(mPostChildEventListener);
现在将RecyclerView设置为
mAllPostAdapter = new AllPostAdapter(MainActivity.this, mPostList);
mPostRecyclerView.setAdapter(mAllPostAdapter);
应该是完美的,但我想使用方法 setEnabled(false); 在适配器上的RecyclerView项目的视图中点击它但我不能,因为你可以看到
我希望 like 按钮是setEnabled(true);当 heatrs 插入DB时,因为在我所设置的点击监听器的onBindViewHolder()方法中喜欢按钮我要查看给定的帖子当前用户喜欢这个位置,那么 like 按钮应该是setEnabled(false);其他行为正常,你可以在这里看到
@Override
public void onBindViewHolder(final PostHolder viewHolder, final int position) {
try{
final Post model = postList.get(position);
viewHolder.likeTextView.setText(String.valueOf(model.getHearts().size()));
// Here I'm if the post at the given position is liked by current user then the _like_ button should be setEnabled(false);
if (!model.hearts.containsKey(currentUserId)){
viewHolder.likeImageView.setOnLikeListener(new OnLikeListener() {
@Override
public void liked(LikeButton likeButton) {
Map<String, Boolean> likeWithUserId = new HashMap<>();
likeWithUserId.put(currentUserId, true);
mPostDatabaseReference.child(model.getPostId())
.child("hearts").setValue(likeWithUserId);
viewHolder.likeTextView.setText(String.valueOf(model.getHearts().size() + 1));
}
@Override
public void unLiked(LikeButton likeButton) {
}
});
} else {
viewHolder.likeImageView.setLiked(true);
viewHolder.likeImageView.setEnabled(false);
}
} catch (Exception e){
e.printStackTrace();
}
}
但你可以看到
行if(model.hearts.containsKey(currentUserId))
没有抓取并导致实时问题,我想在此处解决这个问题,欢迎提出更多问题,我会尝试回复,如果有的话?