Я сделал кнопку, которая при нажатии изменяет значения в базе данных,
статус / цвет кнопки зависит от значения в базе данных.
Как предотвратить обновление всех предметов в обзоре переработчика только потому, что их стоимость изменилась?
Результат, который я получаю:
https://media.giphy.com/media/frHERBcrLUA8x14bsh/giphy.gif
public MainAdapter(Context mContext, List<Posts> mPost)
{
Log.d(TAG, "Main Adapter Start");
this.mContext = mContext;
this.mPost = mPost;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.all_posts_layout, viewGroup, false);
return new MainAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
final Posts post = mPost.get(position);
final String Postkey = post.getPost_uid();
final String likes = Integer.toString(post.getLikes());
holder.setUpStatus(Postkey);
LikesRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
holder.LikepostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
LikeChecker = true;
LikesRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if (LikeChecker.equals(true))
{
if (dataSnapshot.child(Postkey).hasChild(currentUserID)) {
LikesRef.child(Postkey).child(currentUserID).removeValue();
PostsRef.child(Postkey).child("likes").setValue(post.getLikes()-1);
LikeChecker = false;
} else {
LikesRef.child(Postkey).child(currentUserID).setValue(true);
DisikesRef.child(Postkey).child(currentUserID).removeValue();
StoreToHistory(Postkey);
DisikesRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(currentUserID).exists())
{
PostsRef.child(Postkey).child("likes").setValue(post.getLikes()+2);
}
else {
PostsRef.child(Postkey).child("likes").setValue(post.getLikes()+1);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
LikeChecker = false;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}});
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
@Override
public int getItemCount()
{
return mPost.size();
}
ViewHolder:
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
ImageButton LikepostButton, DislikepostButton;
int countLikes, countDislikes, countComms;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
currentUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
LikepostButton = itemView.findViewById(R.id.like_button);
DislikepostButton = itemView.findViewById(R.id.dislike);
CommentPostButton = itemView.findViewById(R.id.comment_button);
DisplayNoOfLikes = itemView.findViewById(R.id.display_number_of_likes);
DisplayNoOfDislikes = itemView.findViewById(R.id.display_number_of_dislikes);
}
public void setUpStatus(final String PostKey)
{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Ups");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(PostKey).hasChild(currentUserID))
{
LikepostButton.setImageResource(R.drawable.upvote);
DisplayNoOfLikes.setTextColor(Color.parseColor("#f14c00"));
}
else
{
LikepostButton.setImageResource(R.drawable.upvote_novote);
}
DisikesRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(PostKey).hasChild(currentUserID))
{
DislikepostButton.setImageResource(R.drawable.downvote_orange);
DisplayNoOfLikes.setTextColor(Color.parseColor("#df3168a7"));
}
else {
DislikepostButton.setImageResource(R.drawable.downvote_novote);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
}); }
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}