ImageView在选择其“兄弟” ImageView之一时改变颜色

问题描述 投票:0回答:1

我遇到的问题是,我有一个comment_item.xml和用户的评论,用户名,喜欢,不喜欢和评论 ImageView,还有一个TextView,用于显示评论,喜欢的次数,以及不喜欢的次数。问题是一个帖子有例如13条评论,而我通过单击ImageView另一条“闪烁”来喜欢或不喜欢其中一条。我的意思是,在我的活动中,我做到了,这样,当您击打时,拇指ImageView从灰色变成红色,如果击打不喜欢ImageView,它从灰色变成黑色,然后得到更新一起喜欢或不喜欢的次数。但是当我按ImageView表示喜欢或不喜欢其他所有评论时,ImageViews也变成了不同的颜色,然后又回到了原来的颜色……这就像是每个人说“眨眼”一样,所以你们理解我所采取的行动的意思。

我如何防止这种情况发生。如果我为其他评论打赞或不喜欢,则所有其他评论的喜欢和不喜欢ImageView

我认为问题可能出在我的xml文件中,但我不是100%。上传了图片,以便大家看到用户界面。现在,当我单击“喜欢”或“不喜欢”时,所有其他“喜欢”和“不喜欢”都将变为闪烁的相反颜色,然后像眨眼一样切换回去。我要避免这种情况发生。如果我在其他帖子上喜欢或不喜欢,他们应该没有反应...

enter image description here

comment_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/image_profile"
        android:layout_width="40dp"
        android:layout_height="40dp" />

    <LinearLayout
        android:id="@+id/linear_layout_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginBottom="10dp"
        android:layout_toEndOf="@+id/image_profile"
        android:orientation="vertical">

        <TextView
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorPrimaryAqua50"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="5" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_layout_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linear_layout_one"
        android:layout_marginBottom="15dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/icon_thumb_up_grey"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginStart="120dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="15dp"
            android:src="@drawable/ic_thumb_up_grey" />

        <ImageView
            android:id="@+id/icon_thumb_up_red"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginStart="120dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="15dp"
            android:src="@drawable/ic_thumb_up_red"
            android:visibility="gone" />

        <TextView
            android:id="@+id/comment_likes_number"
            android:layout_width="16sp"
            android:layout_height="16sp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="15dp" />

        <ImageView
            android:id="@+id/icon_thumb_down_grey"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="15dp"
            android:src="@drawable/ic_thumb_down_grey" />

        <ImageView
            android:id="@+id/icon_thumb_down_black"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="15dp"
            android:src="@drawable/ic_thumb_down_black"
            android:visibility="gone" />

        <TextView
            android:id="@+id/comment_dislikes_number"
            android:layout_width="16sp"
            android:layout_height="16sp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="15dp" />

        <ImageView
            android:id="@+id/icon_comment_grey"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="15dp"
            android:src="@drawable/ic_comment_grey" />

        <ImageView
            android:id="@+id/icon_comment_blue"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="15dp"
            android:src="@drawable/ic_comment_blue"
            android:visibility="gone" />

        <TextView
            android:id="@+id/comment_response_number"
            android:layout_width="16sp"
            android:layout_height="16sp"
            android:layout_marginTop="5dp" />

    </LinearLayout>

</RelativeLayout>

CommentAdapter

       holder.commentLike.setOnClickListener(v -> {

        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();
            }

            notifyDataSetChanged();
        });

        holder.commentDislike.setOnClickListener(v -> {

            if (holder.commentDislike.getTag().equals("dislike")) {
                FirebaseDatabase.getInstance().getReference().child("Comment Liked or Disliked").child(comment.getCommentid()).child("Dislikes").child(mFirebaseUser.getUid()).setValue(true);
                Toast.makeText(mContext, "Don't be mean", Toast.LENGTH_SHORT).show();
            } else {
                FirebaseDatabase.getInstance().getReference().child("Comment Liked or Disliked").child(comment.getCommentid()).child("Dislikes").child(mFirebaseUser.getUid()).removeValue();
            }

            notifyDataSetChanged();
        });


    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_grey);
                            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_black);
                            imageView.setTag("disliked");
                        } else {
                            imageView.setImageResource(R.drawable.ic_thumb_down_grey);
                            imageView.setTag("dislike");
                        }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    }

    private void commentLikesNumber(TextView commentLikesNumber, String commentid) {
        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) {
                commentLikesNumber.setText(dataSnapshot.getChildrenCount() + "");
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    private void commentDislikesNumber(TextView commentDislikesNumber, String commentid) {
        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) {
                commentDislikesNumber.setText(dataSnapshot.getChildrenCount() + "");
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

我遇到的问题是,我有一个comment_item.xml,其中包含用户的评论,用户名,喜欢和不喜欢,并评论ImageView,还有一个TextView,用于评论的数量,喜欢的数量,以及...

java android
1个回答
0
投票
我想,reference.addValueEventListener()中的问题都喜欢和不喜欢;因此,我将修改其中一个,并对另一个进行相同的操作。
© www.soinside.com 2019 - 2024. All rights reserved.