我有一个与BaseExpandableListAdapter相关联的ExpendableListView。在此列表中,当用户单击某个子项时,它会触发一些方法并设置checkBox以进行检查(如果用户再次单击,则同样的事情,但chackBox未经检查)。
为了能够正确使用onChildClick方法,我已将checkBox设置为android:enabled="false"
,如众多帖子所示。
问题是,checkBox会自动变为灰色,检查与否。它不会影响活动的行为,但它不是非常直观(不像检查是浅绿色时)。
如何保持完全相同的行为,但使用彩色复选框?
或者换句话说,是否可以(并且很简单)使用android:enabled="false"
并保持视图的颜色?
以下是checkBox的xml代码:
<CheckBox android:id="@+id/config_can_spn_checkbox"
android:layout_marginRight="30dip"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:enabled="false"
android:focusable="false" />
BaseExpandableListAdapter类:
公共类ConfigCanAdapter扩展了BaseExpandableListAdapter {
private Context context;
private List<Pgn> pgnList;
private LayoutInflater inflater;
public ConfigCanAdapter(Context context, List<Pgn> pgnList) {
this.context = context;
this.pgnList = pgnList;
inflater = LayoutInflater.from(context);
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
public Spn getChild(int gPosition, int cPosition) {
return pgnList.get(gPosition).getSpnList().get(cPosition);
}
public long getChildId(int gPosition, int cPosition) {
return cPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final Spn spn = (Spn) getChild(groupPosition, childPosition);
ChildViewHolder childViewHolder;
if (convertView == null) {
childViewHolder = new ChildViewHolder();
convertView = inflater.inflate(R.layout.group_child, null);
childViewHolder.textViewChildSpnName = (TextView) convertView.findViewById(R.id.config_can_spn_name_text);
childViewHolder.textViewChildSpnUnit = (TextView) convertView.findViewById(R.id.config_can_spn_unit_text);
childViewHolder.checkBoxChildSpn = (CheckBox) convertView.findViewById(R.id.config_can_spn_checkbox);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.textViewChildSpnName.setText(spn.getName());
childViewHolder.textViewChildSpnUnit.setText("(" + spn.getUnit() + ")");
return convertView;
}
public int getChildrenCount(int gPosition) {
return pgnList.get(gPosition).getSpnList().size();
}
public Object getGroup(int gPosition) {
return pgnList.get(gPosition);
}
public int getGroupCount() {
return pgnList.size();
}
public long getGroupId(int gPosition) {
return gPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder gholder;
Pgn pgn = (Pgn) getGroup(groupPosition);
if (convertView == null) {
gholder = new GroupViewHolder();
convertView = inflater.inflate(R.layout.group_row, null);
gholder.textViewGroup = (TextView) convertView.findViewById(R.id.TVGroup);
convertView.setTag(gholder);
} else {
gholder = (GroupViewHolder) convertView.getTag();
}
gholder.textViewGroup.setText(pgn.getId());
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
class GroupViewHolder {
public TextView textViewGroup;
}
class ChildViewHolder {
public TextView textViewChildSpnName;
public TextView textViewChildSpnUnit;
public CheckBox checkBoxChildSpn;
}
}
最后是onChildClickListener:
expandableList.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox)v.findViewById(R.id.config_can_spn_checkbox);
cb.toggle();
Toast.makeText(getBaseContext(), "Group = " + String.valueOf(groupPosition) + " child = " + String.valueOf(childPosition), Toast.LENGTH_SHORT).show();
return false;
}
});
答案 0 :(得分:0)
您可以将按钮的选择器覆盖为以下内容:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/image_enabled" />
<item android:drawable="@drawable/image_enabled" />
</selector>
答案 1 :(得分:0)
您可以使用点击侦听器来拦截点击并更改值,而不是禁用该复选框。这听起来像是一个黑客;你也可以将视图子类化并以这种方式覆盖不良行为。
答案 2 :(得分:0)
您可以设置android:clickable="false"
和android:focusable="false"
,它会占用所有点击次数而不会占用焦点但保持enabled
看起来像您想要的