클릭 할 때 버튼의 색상을 변경하고 다음 클릭에서 기본 색상으로 되 돌리는 방법은 무엇입니까?

시야

나는이 like내에서 버튼을 RecyclerView, 내가 원하는 사용자가 쳤을 때이다 like1 시간 동안 버튼을, 버튼 배경 색상이 변경됩니다 red색상, 동일한 사용자가 쳤을 때 like버튼을, 버튼이 기본 색으로 다시 변경됩니다 white.

나는 몇 가지 질문을 확인했지만 여전히 내가 원하는 것을 얻지 못했습니다. 지금까지 내 솔루션은 아래와 같으며 오류가 발생하지 않지만 버튼을 클릭해도 아무 일도 일어나지 않습니다.

 likeButton =(Button) view.findViewById(R.id.likeButton);

 //here for user like the post
 holder.likeButton.setOnClickListener(new View.OnClickListener() {
            boolean clicked = true;

            @Override
            public void onClick(View v) {
                if(!clicked){
                    holder.likeButton.setBackgroundColor(Color.RED);
                    clicked = true;

                    //here i will update the database

                }else{
                    holder.likeButton.setBackgroundColor(Color.WHITE);
                    clicked = false;
                     //here i will update the database
                }


            }
        });

나는이 대답확인 했기 때문에 아래 코드를 수정했지만 버튼을 클릭해도 아무 일도 일어나지 않습니다.

 holder.likeButton.setBackgroundColor(Color.WHITE);
 holder.likeButton.setOnClickListener(new View.OnClickListener() {
        ValueAnimator buttonColorAnim = null;

        @Override
        public void onClick(View v) {
            if(buttonColorAnim != null){
                buttonColorAnim.reverse();
                buttonColorAnim = null;
              //here i will update the database
            }else{
                final Button button = (Button) v;//here is the line I dont undestand
                buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.WHITE);

                buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animator) {
                        // set the background color
                        button.setBackgroundColor((Integer) animator.getAnimatedValue());
                    }
                  //here i will update the database
                });

                buttonColorAnim.start();
            }
        }
    });

누군가 내가 놓친 것을 지적하십시오. 원하는 것은 처음 클릭 할 때 프로그래밍 방식으로 버튼 색상을 변경하고 다음 클릭을 위해 기본값으로 다시 변경하는 것입니다 (동일한 사용자가 여러 번 클릭하는 것을 피함).

딜립 파텔

안녕하세요, 이것이 당신을 도울 수 있기를 바랍니다.

In XML

  <Button
    android:id="@+id/btnClick"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:text="click"/>

어댑터 클래스에서

  boolean click = true;


        holder.btnClick.setTag(position);
        holder.btnClick.setId(position);
        holder.btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (click) {
                    holder.btnClick.setBackgroundColor(Color.RED);
                    click = false;
                } else {
                    holder.btnClick.setBackgroundColor(Color.WHITE);
                    click = true;
                }
                notifyDataSetChanged();
            }
        });

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관