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回目のクリックであるとき、私がしたいことは、プログラムチェンジボタンの色であり、(同じユーザからのように複数のを避けている)次のクリックのために戻ってデフォルトに変更。
あなたの最初の例正しいように見える、しかし、あなたの 'clicked'変数は値' true'を割り当てていますつまり、 '!clicked'は常にfalseであり、あなたのコードは決して実行されません。 – MatusMak
私は最初の例にfalseを割り当てる必要がありますか? @MatusMak – ken
downvoteの理由は? – ken