2017-04-10 27 views
-2

likeボタンがRecyclerViewにあります。ユーザーが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'は常にfalseであり、あなたのコードは決して実行されません。 – MatusMak

+0

私は最初の例にfalseを割り当てる必要がありますか? @MatusMak – ken

+0

downvoteの理由は? – ken

答えて

0

こんにちは

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

...これはあなたを助けることができる、この希望にしてみてください

インアダプタクラス

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操作。だから、クリックリスナーでは、以前のユーザーのようなデータを取得し、バックグラウンドを変更し、新しいクリックごとにデータベースを更新します。主なレイアウトでごrow.xmlファイルに次の行を追加すること

0

試してみてください。

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> 
関連する問題