【发布时间】:2017-09-05 09:33:01
【问题描述】:
我的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();
}
}
});
有人请指出我缺少的东西,我想要的是在第一次点击时以编程方式更改按钮颜色,并在下次点击时更改回默认值(避免来自同一用户的多次喜欢)。
【问题讨论】:
-
您的第一个示例似乎是正确的,但是,您的
clicked变量已分配值true,这意味着!clicked始终为 false,并且您的代码永远不会执行。 -
所以我应该为第一个示例分配 false 吗? @MatusMak
-
投反对票的原因?
-
这不是我提出的,我不是因为它不鼓励人们提出问题而对问题投反对票,但我猜有人不喜欢你写问题的方式。
标签: android android-layout android-widget