Cómo puedo hacer que solo se pueda seleccionar un ImageView
no ambos. Tengo dos ImageViews
, uno representa el botón like y el otro el botón dislike. El usuario solo puede elegir uno no ambos para un post obviamente. El código no me está dando el resultado esperado. Espero que alguien me pueda señalar lo que estoy haciendo mal.
CommentAdapter
private ImageView img1, img2;
img1 = view.findViewById(R.id.icon_thumb_up_black);
img2 = view.findViewById(R.id.icon_thumb_down_black);
holder.commentLike.setOnClickListener(v -> {
if (img1.isSelected()) {
img1.setSelected(false);
} else {
img1.setSelected(true);
img2.setSelected(false);
}
if (holder.commentLike.getTag().equals("like")) {
FirebaseDatabase.getInstance().getReference().child("Comment Liked or Disliked").child(comment.getCommentid()).child("Likes").child(mFirebaseUser.getUid()).setValue(true);
} else {
FirebaseDatabase.getInstance().getReference().child("Comment Liked or Disliked").child(comment.getCommentid()).child("Likes").child(mFirebaseUser.getUid()).removeValue();
}
});
holder.commentDislike.setOnClickListener(v -> {
if (img2.isSelected()) {
img2.setSelected(false);
} else {
img1.setSelected(false);
img2.setSelected(true);
}
if (holder.commentDislike.getTag().equals("dislike")) {
FirebaseDatabase.getInstance().getReference().child("Comment Liked or Disliked").child(comment.getCommentid()).child("Dislikes").child(mFirebaseUser.getUid()).setValue(true);
} else {
FirebaseDatabase.getInstance().getReference().child("Comment Liked or Disliked").child(comment.getCommentid()).child("Dislikes").child(mFirebaseUser.getUid()).removeValue();
}
});
comment_item
<ImageView
android:id="@+id/icon_thumb_up_black"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/linear_layout_one"
android:layout_centerInParent="true"
android:layout_marginTop="5dp"
android:src="@drawable/ic_thumb_up_black" />
<ImageView
android:id="@+id/icon_thumb_up_red"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/linear_layout_one"
android:layout_centerInParent="true"
android:layout_marginTop="5dp"
android:src="@drawable/ic_thumb_up_red"
android:visibility="gone" />
<ImageView
android:id="@+id/icon_thumb_down_black"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/linear_layout_one"
android:layout_centerInParent="true"
android:layout_marginStart="35dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="@+id/icon_thumb_up_black"
android:src="@drawable/ic_thumb_down_black" />
<ImageView
android:id="@+id/icon_thumb_down_red"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/linear_layout_one"
android:layout_centerInParent="true"
android:layout_marginStart="35dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="@+id/icon_thumb_up_black"
android:src="@drawable/ic_thumb_down_red"
android:visibility="gone" />
private void commentLike(final String commentid, final ImageView imageView) {
if (commentid != null) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Comment Liked or Disliked").child(commentid).child("Likes");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (mFirebaseUser != null)
if (dataSnapshot.child(mFirebaseUser.getUid()).exists()) {
imageView.setImageResource(R.drawable.ic_thumb_up_red);
imageView.setTag("liked");
} else {
imageView.setImageResource(R.drawable.ic_thumb_up_black);
imageView.setTag("like");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
private void commentDislike(final String commentid, final ImageView imageView) {
if (commentid != null) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Comment Liked or Disliked").child(commentid).child("Dislikes");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (mFirebaseUser != null)
if (dataSnapshot.child(mFirebaseUser.getUid()).exists()) {
imageView.setImageResource(R.drawable.ic_thumb_down_red);
imageView.setTag("disliked");
} else {
imageView.setImageResource(R.drawable.ic_thumb_down_black);
imageView.setTag("dislike");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}