我有RecyclerView
一个文本视图和一个自定义CheckBoxView
类,它可以点击并且具有焦点,并在适配器中实例化。所有逻辑都在这个类中。
现在,当单行具有焦点时,复选框为黄色,单击时为灰色。当我点击另一个项目时问题就出现了,因为之前点击的项目保存为灰色不会转到空图像,但保持原状。
单击另一个CheckBox时它应该为空。
public class MyCheckBoxView {
public enum CheckType {
ACTIVE, // YELLOW When check box has focus on
SELECTED, // GRAY When user selects view
EMPTY // // EMPTY When not selected
}
private CheckType checkType;
private ImageView view;
private boolean isChecked = false;
/**
* Constructor
*/
public MyCheckBoxView() {
setup();
}
//Setup method: Fill In View
private void setup() {
//Create view
view = new ImageView(getContext());
view.setFocusable(true);
view.setClickable(true);
setCheckType(CheckType.EMPTY);
view.setImageResource(R.drawable.check_box_empty);
//Set CheckBox focus behaviours
view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
checkType = CheckType.ACTIVE;
view.setImageResource(R.drawable.check_box_active);
} else {
// NO FOCUS
view.setImageResource(R.drawaable.check_box_empty);
// If checkbox view is checked
if (isChecked) {
view.setImageResource(R.drawable.check_box_selected);
checkType = CheckType.SELECTED;
} else {
if (checkType == CheckType.SELECTED) {
view.setImageResource(R.drawable.check_box_selected);
isChecked = true;
} else {
view.setImageResource(R.drawable.check_box_empty);
isChecked = false;
}
}
}
}
});
//Set CheckBox state on click event
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isChecked) {
view.setImageResource(R.drawable.check_box_selected);
checkType = CheckType.SELECTED;
} else {
}
}
});
}
//GET VIEW - Called in Adapter
public View getView() {
return view;
}
//Set Check Type
public void setCheckType(CheckType checkType) {
this.checkType = checkType;
}}
答案 0 :(得分:0)
你应该做这样的事情
当选中某个复选框时,从每个复选框中删除灰色,然后将灰色应用于单击的复选框。当你检查它时,你将获得点击和检查的ID复选框,您可以通过setOnClickListener
将其设置为灰色!确保添加
view.setImageResource(R.drawable.beeline_check_box_selected);
checkType = CheckType.SELECTED;
删除所有灰色后!