【问题标题】:selected item on custom listview with contextual action bar带有上下文操作栏的自定义列表视图上的选定项目
【发布时间】:2012-03-05 07:25:25
【问题描述】:

我最近开始使用 android actionbars 和 contextual action bars (CAB)。

我只有一项活动,即 ListActivity。基本上我使用以下代码来“激活”CAB:

ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position,
                                      long id, boolean checked) {
        // Here you can do something when items are selected/de-selected,
        // such as update the title in the CAB
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // Respond to clicks on the actions in the CAB
        switch (item.getItemId()) {
            case R.id.menu_delete:
                deleteSelectedItems();
                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate the menu for the CAB
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context, menu);
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // Here you can make any necessary updates to the activity when
        // the CAB is removed. By default, selected items are deselected/unchecked.
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // Here you can perform updates to the CAB due to
        // an invalidate() request
        return false;
    }
});

列表的布局:

<ImageView
    android:id="@+id/message_on_clipboard_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:minWidth="30dp"
    android:padding="7sp"
    android:src="@android:drawable/presence_online" >
</ImageView>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listitem_background"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="5sp"
        android:fadingEdge="horizontal"
        android:singleLine="true"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceLarge" >
    </TextView>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/listitem_background"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/message_length"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5sp"
            android:text="@string/message_length"
            android:textAppearance="?android:attr/textAppearanceSmall" >
        </TextView>

        <TextView
            android:id="@+id/message_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5sp"
            android:text="TextView"
            android:textAppearance="?android:attr/textAppearanceSmall" >
        </TextView>

        <TextView
            android:id="@+id/date_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5sp"
            android:text="@string/date_label" >
        </TextView>

        <TextView
            android:id="@+id/date_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" >
        </TextView>
    </LinearLayout>
</LinearLayout>

在 main.xml 中:

<ListView
    android:id="@+android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>
</ListView>

现在,如果我长按列表项,CAB 会按预期显示:

我使用 MultiChoiceModeListener 但不幸的是,所选列表项不会像此处示例中那样更改背景(选择项目后为浅蓝色背景):

我必须使用自定义选择器吗?或者是否有一个标准程序,android如何处理这个,我只需要让我的LinearLayouts透明?我也尝试了以下但没有成功:

ListView item background via custom selector

如果有人能指出我正确的方向,那就太好了。如果您需要更多应用程序代码或 xml 文件,请告诉我。

【问题讨论】:

    标签: android android-layout android-listview android-linearlayout


    【解决方案1】:

    我只在 CHOICE_MODE_SINGLE 中对此进行过测试,但在这种情况下,它可以通过执行以下操作来工作。

    • 当您选择列表项时,在代码中,为列表中的该项调用“setItemChecked(position, checked)”方法(在 ListView 实例上)。

    • 将此添加到各个 ListView 项的 XML:
      android:background="?android:attr/activatedBackgroundIndicator"

    【讨论】:

    • 如果我在 onItemCheckedStateChanged() 内执行以下操作,我会收到 StackOverflowError:listView.setItemChecked(position, checked);
    • 非虚拟机。它只适用于:android:background="?android:attr/activatedBackgroundIndicator" 非常感谢!
    • 调用 setItemChecked 会导致 StackOverflowError,因为在 onItemCheckedStateChanged 中调用 setItemChecked 只会无限调用对方。
    • 对于列表listChoiceBackgroundIndicator 是更好的选择。
    • 对@domi,通过设置此listView.setItemChecked(position, checked);,您一次又一次地调用onItemCheckedStateChanged 方法,导致对该方法的无休止调用,最终导致StackOverflowError。希望它能解决您的问题。
    【解决方案2】:

    只需在途中创建一个名为 custom_background 的可绘制对象:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/highlight" android:state_activated="true"/>
        <item android:drawable="@color/normal"/>
    </selector>
    

    并设置为父布局的背景:

    android:background="@drawable/custom_background"
    

    【讨论】:

    • 完美!我创建了选择器并将其应用为网格视图项目背景。谢谢。
    【解决方案3】:

    仅尝试支持 ICS 设备及更高版本,但这种新布局可以实现您想要实现的目标,即突出显示选定的行。

    adapter = new SimpleCursorAdapter(getActivity(),
         android.R.layout.simple_list_item_activated_1, 
         null, 
         from, 
         to,
         0);
    

    【讨论】:

      【解决方案4】:

      对于仍然遇到此问题的任何人,请检查您的 android:minSdkVersion。如果它太低,选择器背景颜色可能不起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-10
        • 2015-08-12
        • 2023-03-25
        • 2015-07-16
        • 2013-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多