状态为false

时间:2015-09-08 20:30:16

标签: android listview checkedtextview

我有listviewCheckedTextViews作为记录,点击某个项目时我可以看到CheckedTextView的状态已切换但Checkmark永远不会隐藏

这是我的listView的XML

<ListView
    android:id="@+id/listFilters"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="10dp"
    android:choiceMode="multipleChoice" />

这是记录的XML:

<CheckedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/filter"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:minHeight="60dp"
    android:text="filter1"
    android:paddingStart="20dp"
    android:paddingEnd="20dp"
    android:gravity="center_vertical"
    android:checkMark="@drawable/icon_blue_checkmark" />

这是我的OnItemClick Listener :

@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
    Filter filter = (Filter) adapter.getItem (position);
    boolean isSelected = !filter.isSelected ();
    filter.setSelected (isSelected);

    CheckedTextView ctv = (CheckedTextView)listView.getChildAt (position);
    ctv.toggle ();

    // shows that the status of the CheckedTextView has switched successfully
    Toast.makeText (
        getActivity (),
        "CheckedTextView status : " + ctv.isChecked (),
        Toast.LENGTH_SHORT
    ).show();
}

我做错了什么?感谢。

编辑: 我刚刚注意到,当用android:checkMark="?android:attr/listChoiceIndicatorMultiple"替换我的自定义复选标记时,它工作正常。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

试试这个

CheckedTextView ctv = (CheckedTextView) view.findViewById(R.id.filter);
ctv.toggle();

答案 1 :(得分:0)

here所述,在声明适配器时可以尝试使用 import org.apache.catalina.deploy.ContextEnvironment; import org.apache.catalina.deploy.ContextResource; import org.apache.catalina.deploy.NamingResources;