[英]Android cardview, change background color with switch listener
I have a cardview with a switch inside. 我有一个Cardview,里面有一个开关。 When the switch is on, I want to change the background color of the card to green, when is off(by default it is) instead is red.
当开关打开时,我想将卡的背景颜色更改为绿色,当它关闭时(默认为)改为红色。 I also need to save the switch state locally, so when I open the app again, it remain in the last position it was.
我还需要将开关状态保存在本地,因此当我再次打开应用程序时,它保持在原来的位置。 I don't know if it's ok doing all the code for that in the adapter..I'm using 2 fragment, and the cardview is inside one of them, in a listview.
我不知道是否可以在适配器中完成所有代码。.我正在使用2个片段,而cardview位于其中一个列表视图中。 I think I have to use
我想我必须用
// paidSwitch is the switch in the cardview paidSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { paidCard.setCardBackgroundColor(??? I need to get my R.color.green value here);
This code is inside the getView method in the adapter class. 这段代码在适配器类的getView方法内部。 Each cardview in the list has it's own switch.
列表中的每个cardview都有其自己的开关。 I attach a picture of the fragments, so it's easier to understand.
我附上碎片的图片,因此更容易理解。 ps.
PS。 How can I reduce the size(screen size) of the picture when I post some photo here?
在此处张贴一些照片时,如何减小照片的尺寸(屏幕尺寸)?
This should work : 这应该工作:
final int greenBackgroundColor = ContextCompat.getColor(this, R.color.my_red_color);
final int redBackgroundColor = ContextCompat.getColor(this, R.color.my_green_color);
switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
cardView.setCardBackgroundColor(greenBackgroundColor);
}
else{
cardView.setCardBackgroundColor(redBackgroundColor);
}
}
});
And set the default card background with : 并使用设置默认的卡片背景:
app:cardBackgroundColor="@color/white"
Hope this helps 希望这可以帮助
To save state locally, you can use SharedPrefrences or Paper library. 要在本地保存状态,可以使用SharedPrefrences或Paper库。 I personally prefer Paper.
我个人更喜欢Paper。
You can use it like so 您可以像这样使用
//Init Paper
Paper.init(this);
//save switch state
Paper.book().write("key1", "value1");
Paper.book().write("key2", "value2");
// key being the switch Indentifier, value being the state of the specific switch
//save switch state
String value1 = Paper.book().read("key1");
String value2= Paper.book().read("key2");
//delete all saved record
Paper.book().destroy();
EDIT -- 编辑-
This logic of Saving the state can be implemented after the if-else of the onCheckedChanged, like so 这种保存状态的逻辑可以在onCheckedChanged的if-else之后实现,如下所示
if (isChecked){
//line of code to change color
...
}
else{
//line of code to change color
...
}
//line of code to save state
Paper.book().write("position", isChecked);
This logic of Reading the state & Setting to switches can be implemented in onResume (and onCreate too, if needed) , like so 可以在onResume(和onCreate(如果需要)中)中实现这种读取开关状态和设置的逻辑,就像这样
paidSwitchAtPosition.setChecked(Paper.book().read("position"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.