How to change color of button when being click,and

2019-03-07 02:42发布

I have a like button in my RecyclerView,what I want is when user hit the like button for the 1st time,the button background color will change to red color,and when the same user hit the like button,the button will change back to default color which is white.

I checked for few SO question,but still havent get what I want.So far my solution is like below,doesnt produce any error but when clicked the button,nothing happen.

 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
                }


            }
        });

I checked this SO answer too,so I modified my code as below,but still nothing happens when the button is clicked.

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

Somebody please point out what I'm missing,what I want is change button color programmatically when being click for 1st time,and change back to default for next click(which avoid multiple like from a same user).

5条回答
趁早两清
2楼-- · 2019-03-07 03:12

Instead of clicked or not condition, make and use condition from you update database for like and dislike operation. So, In click-listener get data previously user like or not then change background and update database as per new click.

查看更多
一纸荒年 Trace。
3楼-- · 2019-03-07 03:20

Try adding this line to your row.xml file in the main layout :

android:descendantFocusability="blocksDescendants"
查看更多
冷血范
4楼-- · 2019-03-07 03:23

You should create a selector file. Create a file in drawable folder like color_change.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@color/button_pressed"/> <!-- pressed -->
<item android:state_focused="true"
    android:drawable="@color/button_focused"/> <!-- focused -->
<item android:drawable="@color/button_default"/> <!-- default -->
</selector>

and declare it in the button like this

 <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/color_change"
    android:text="Click Me" />
查看更多
趁早两清
5楼-- · 2019-03-07 03:28

Have a look at this. Here, I changed the button text color on click. First time, all buttons appear white and after click it toggles between red and white as you expected. --

//LikeAdapter.java

public class LikeAdapter extends RecyclerView.Adapter<LikeAdapter.LikeHolder> {

    public LikeAdapter() {

    }

    @Override
    public LikeAdapter.LikeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.like_item,parent,false);
        return new LikeHolder(view);
    }

    @Override
    public void onBindViewHolder(final LikeAdapter.LikeHolder holder, int position) {

        holder.red_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (holder.red_btn.getVisibility() == View.VISIBLE) {
                    holder.red_btn.setVisibility(View.GONE);
                    holder.white_btn.setVisibility(View.VISIBLE);
                } else {
                    holder.red_btn.setVisibility(View.VISIBLE);
                    holder.white_btn.setVisibility(View.GONE);
                }
            }
        });

        holder.white_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.white_btn.getVisibility() == View.VISIBLE) {
                    holder.red_btn.setVisibility(View.VISIBLE);
                    holder.white_btn.setVisibility(View.GONE);
                } else {
                    holder.red_btn.setVisibility(View.GONE);
                    holder.white_btn.setVisibility(View.VISIBLE);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    public class LikeHolder extends RecyclerView.ViewHolder {
        private Button red_btn, white_btn;
        public LikeHolder(View itemView) {
            super(itemView);
            red_btn = (Button) itemView.findViewById(R.id.red_btn);
            white_btn = (Button) itemView.findViewById(R.id.white_btn);
            red_btn.setBackgroundColor(Color.RED);
            white_btn.setBackgroundColor(Color.WHITE);

        }
    }
}

//like_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">



    <Button
        android:id="@+id/red_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Red"/>

    <Button
        android:id="@+id/white_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="White"/>



</RelativeLayout>
查看更多
不美不萌又怎样
6楼-- · 2019-03-07 03:34

Hi try to this hope this can help you...

In XML

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

In Adapter Class

  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();
            }
        });
查看更多
登录 后发表回答