使用上下文操作栏在自定义列表视图上选择的项目

dom*_*omi 42 android android-layout android-listview android-linearlayout

我最近开始使用机器人为其他动作上下文的动作条(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;
    }
});
Run Code Online (Sandbox Code Playgroud)

列表的布局:

<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>
Run Code Online (Sandbox Code Playgroud)

在main.xml中:

<ListView
    android:id="@+android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>
</ListView>
Run Code Online (Sandbox Code Playgroud)

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

出租车

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

在此输入图像描述

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

通过自定义选择器ListView项目背景

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

Ale*_*cas 53

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

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

  • 将其添加到单个ListView项的XML中:
    android:background="?android:attr/activatedBackgroundIndicator"

  • 对setItemChecked的调用将导致StackOverflowError,因为在onItemCheckedStateChanged中调用setItemChecked将只是无限地相互调用. (9认同)
  • NVM.它适用于:`android:background ="?android:attr/activatedBackgroundIndicator""非常感谢你! (8认同)
  • 如果我在`onItemCheckedStateChanged()`中执行以下操作,我会得到一个StackOverflowError:`listView.setItemChecked(position,checked);` (6认同)

小智 12

只需在路上创建一个名为custom_background的drawable:

<?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>
Run Code Online (Sandbox Code Playgroud)

并在父布局上设置为背景:

android:background="@drawable/custom_background"
Run Code Online (Sandbox Code Playgroud)