2017-04-10 68 views
-2

我的RecyclerView中有一个like按钮,我想要的是当用户第一次点击like按钮时,按钮背景颜色将变为red颜色,当同一用户按下like按钮时,按钮将会改变回到默认颜色即white单击时如何更改按钮的颜色,并在下次单击时恢复为默认颜色?

我检查了几个SO问题,但仍然没有得到我想要的。到目前为止,我的解决方案如下,没有产生任何错误,但点击按钮时,什么也没有发生。

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 
       } 


      } 
     }); 

我检查这个SO answer太多,所以我修改我的代码如下,但是单击按钮时仍然没有任何反应。

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

有人请指出什么我失踪,我要的是改变按钮的颜色是编程时点击第1次,并改回默认为下一个点击(这避免这样多来自同一个用户) 。

+0

你的第一个例子似乎是正确的,但是,你的'clicked'变量赋值TRUE; ,这意味着'!clicked'总是假的,你的代码永远不会被执行。 – MatusMak

+0

所以我应该为第一个例子分配false? @MatusMak – ken

+0

downvote的原因? – ken

回答

0

嗨尝试这种希望这可以帮助你......

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

您应该创建一个选择器文件。在创建文件夹绘制一个文件中像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> 

,并宣布它的按钮这样

<Button 
    android:id="@+id/button1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/color_change" 
    android:text="Click Me" /> 
0

代替clicked与否的条件,制造和使用条件从您like更新数据库dislike操作。因此,在点击监听器中获取之前用户喜欢或不喜欢的数据,然后根据新的点击更改背景并更新数据库。

0

尝试在主要布局中添加这一行到您的row.xml文件:

android:descendantFocusability="blocksDescendants" 
+0

这一个是为了什么雅? – ken

+0

为了使按钮可点击,它只是把焦点放在按钮上而不是你的列表中。你试过了吗? –

+0

我只是试过,但没有任何效果 – ken

0

看看这个。在这里,我点击了按钮文字颜色。第一次,所有按钮都显示为白色,点击后按照您的预期在红色和白色之间切换。 -

//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>