1

可能重复:
退出选择模式后,ListView 选择保持不变

我的应用程序中有一个列表视图,它在 listitem_row.xml 中包含多个组件(我为每个项目使用的布局)。我面临的问题是我有一个用于选择每个项目的 ActionMode。当用户选择任何项目时,将创建操作模式并按预期工作。当我关闭动作模式时,我想让项目看起来像选择之前一样未选择/突出显示/激活/检查。这是我正在使用的代码:

if (selectedView.isPressed()) {
    selectedView.setPressed(false);
}
if (selectedView.isActivated()) {
    selectedView.setActivated(false);
}
if (selectedView.isSelected()) {
    selectedView.setSelected(false);
}
if (selectedView.isFocused()) {
    selectedView.clearFocus();
    selectedView.dispatchWindowFocusChanged(false);
}
selectedView.refreshDrawableState();
selectedView.requestLayout();
if (selectedView.isDirty()) {
    selectedView.postInvalidate();
}

为了信息的缘故:我单独尝试了所有组合。我上面粘贴的代码是我在似乎没有任何工作时达到的状态。我使用调试检查了所有状态,当用户单击关闭操作模式按钮时,唯一处于真实状态的状态是激活状态。

这是我的列表视图行的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/innerrow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/listview_selector"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/listViewHeading"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/mydrawable"
            android:drawablePadding="5sp"
            android:gravity="center|left"
            android:paddingBottom="5sp"
            android:paddingLeft="15sp"
            android:paddingRight="15sp"
            android:paddingTop="5sp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/app_text_color"
            android:textStyle="bold" />

        <View
            style="@style/line"
            android:layout_width="match_parent"
            android:layout_height="1dp" />

        <TextView
            android:id="@+id/listViewDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="15sp"
            android:paddingLeft="15sp"
            android:paddingRight="15sp"
            android:paddingTop="5sp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/app_text_color" />
    </LinearLayout>

</LinearLayout>

任何建议/反馈将不胜感激。

4

1 回答 1

0

For any ListView item, which already has a single choice mode for it, only two lines are required for the removal of the selection:

myListView.clearChoices();
myListView.requestLayout();

This solved my problem.

于 2012-10-06T12:32:07.080 回答