我使用的是自定义适配器,用于我的ListView。创建ArrayList后
WifiListAdapter<WifiListItem> adapter = new WifiListAdapter<WifiListItem>(
this, R.layout.listview_item_text, listItems);我的应用程序中有以下代码:
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);但是,当我尝试单击复选框时,什么都不会发生。因此,我必须设法手动切换复选框状态。
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView checkBox = (CheckedTextView) v
.findViewById(R.id.text1);
if (checkBox != null) {
checkBox.toggle();那就成功了。(在此之前,我必须删除setChoiceMode方法调用)
那么有什么问题呢?为何没有效果?
我的R.layout.listview_item_text.xml
<TextView
android:id="@+id/topText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:paddingTop="7dp"
android:textColor="?android:attr/textColorPrimary"/>
<TextView
android:id="@+id/botText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:paddingTop="2dp"
android:paddingBottom="7dp"
android:textColor="?android:attr/textColorTertiary"
/>在我的适配器中,我使用以下代码膨胀复选框布局:
convertView = mInflater.inflate(
R.layout.listview_item_checkbox, null);
CheckedTextView checkbox = ((CheckedTextView) convertView
.findViewById(R.id.text1))listview_item_checkbox.xml
<CheckedTextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="22sp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>发布于 2012-12-20 11:20:28
而不是您的代码:
if (checkBox != null)
checkBox.toggle();试着做这样的事情:
if (checkBox != null) checkBox.setChecked(l.getCheckedItemPositions().get(position));如果可以--改进您的代码(创建CheckableFrameLayout作为列表项的父级布局)。ListView保留检查位置的BooleanSparseArray (您可以通过方法getCheckedItemPositions()获得它)。在代码(grepcode)中,只有当View正在实现可校验时,它才会将View设置为检查或不单独设置。如果将CheckableFrameLayout和sat创建为主父级,则不必在适配器中处理这些情况。
发布于 2012-12-20 09:27:59
我认为您必须使用实现可校验的小部件。试着用android.R.layout.simple_list_item_multiple_choice。如果需要两行,那么实现类似的内容。
发布于 2013-09-04 14:00:53
问题是,源代码中存在一些问题(在我编写这个答案时,事件发生在最新版本,4.3r2.1)。以下是问题所在:
1899 if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
1900 if (child instanceof Checkable) {
1901 ((Checkable) child).setChecked(mCheckStates.get(position));
1902 } else if (getContext().getApplicationInfo().targetSdkVersion
1903 >= android.os.Build.VERSION_CODES.HONEYCOMB) {
1904 child.setActivated(mCheckStates.get(position));
1905 }
1906 }源代码:r2.1/android/widget/ListView.java?av=f
对于您的问题,如果您的minSDK是11,最简单的答案是按以下方式自定义复选框:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true" android:drawable="@drawable/checkbox_selected"/>
<item android:state_activated="false" android:drawable="@drawable/checkbox_deselected" />
<item android:state_checked="false" android:drawable="@drawable/checkbox_deselected" />
<item android:state_checked="true" android:drawable="@drawable/checkbox_selected" />
</selector>将其保存到可绘制文件夹,作为xml文件,并将其作为布局引用。
中间级别的解决方案设置为onItemClickListener,并根据选择模式执行不同的行为。
下面是工作的源代码:https://github.com/jiahaoliuliu/CustomizedListRow
很难但正确的解决方案是来自MarvinLab的解决方案,您可以在这里找到它:http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/
https://stackoverflow.com/questions/13968390
复制相似问题