2

我有一个主细节 ListView 设置,我想更改主片段中选定项目的背景。

我曾经有以下代码,但这不允许我使用成熟的选择器:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        // Highlight selected item
        if (position == selectedPos) {
            view.setBackgroundColor((view.getResources()
            .getColor(R.color.nw_white)));
        } else {
            view.setBackgroundColor((view.getResources()
            .getColor(R.color.nw_grey)));
        }
        return view;
    }

它工作正常,但我想使用选择器。(因为我希望项目在焦点和点击时适当地改变背景。)

所以我把它改成:

            @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        // Highlight selected item
        if (position == selectedPos) {
            view.setSelected(true);
            // view.setBackgroundColor((view.getResources()
            // .getColor(R.color.nw_row_download_master_selected)));
        } else {
            view.setSelected(false);
            // view.setBackgroundColor((view.getResources()
            // .getColor(R.color.nw_row_download_master_normal)));
        }
        Log.d(TAG, "pos " + position + " is selected: "
            + view.isSelected());

        return view;
    }

列表行如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_row_collection_master"
    android:orientation="horizontal"
    >

   ...
</LinearLayout>

和这样的选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/row_master_detail_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/row_master_detail_focused"
          android:state_focused="true" />
    <item android:drawable="@color/nw_row_master_master_selected"
          android:state_selected="true" />
    <item android:drawable="@color/nw_row_master_normal" />
</selector>

该行响应单击事件但不响应被选中,即使记录器说视图确实被选中!到底是怎么回事?

4

3 回答 3

4

我不知道为什么视图不会显示为选中状态,即使它设置为选中状态,但这段代码应该可以工作:

使用ListView.setItemChecked()

mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setItemChecked(position, isActivated);

选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/row_master_detail_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/row_master_detail_focused"
          android:state_focused="true" />
    <item android:drawable="@color/nw_row_master_master_selected"
          android:state_activated="true" />
    <item android:drawable="@color/nw_row_master_normal" />
</selector>

因为android:state_activated仅在蜂窝后可用,所以将其添加到getView()

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                // Highlight selected item
                if (position == selectedPos) {
                    view.setBackgroundColor((view.getResources()
                            .getColor(R.color.nw_row_master_selected)));
                } else {
                    Drawable sel = view.getResources().getDrawable(
                            R.drawable.selector_row_collection_master);
                    view.setBackgroundDrawable(sel);
                }
            }
于 2013-08-20T10:14:04.027 回答
1

您必须selector为您设置item view并删除selector您的list-view. 只需将您list-view的设置selector@null. 希望这可以帮助。

于 2013-08-20T10:05:32.483 回答
1

您应该使用state_activated,而不是state_selected查看https://stackoverflow.com/a/13634298/1283554

  • 使您的列表可供选择,例如设置 android:choiceMode="singleChoice"ListView

  • 使用带有'state_activated="true"状态的选择器作为项目背景

  • ListView使用myListView.setItemChecked(position, true)选择项目

注意:state_activated仅从 Honeycomb 开始可用。对于旧版本,我认为你不能只使用选择器来做到这一点。您可能必须将背景设置为 getView

于 2013-08-20T10:15:12.933 回答