我有一个与 BaseExpandableListAdapter 关联的 ExpendableListView。在这个列表中,当用户单击一个孩子时,它会触发一些方法并将一个复选框设置为选中(如果用户再次单击,则同样的事情,但 chackBox 未选中)。
为了能够正确使用 onChildClick 方法,我已将 checkBox 设置为android:enabled="false"
,正如许多帖子所建议的那样。
问题是,复选框自动变为灰色,无论是否选中。它不会影响活动的行为,但不是很直观(与检查为浅绿色时不同)。
我怎样才能保持完全相同的行为,但使用彩色复选框?
或者换句话说,是否可以(并且简单)使用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;
}
});