【问题标题】:Highlight selected item in ListView on Android在 Android 上的 ListView 中突出显示所选项目
【发布时间】:2014-03-03 18:24:42
【问题描述】:

我一直在制作一个适用于 Android 中的 ListViews 的应用程序,但我无法使所选(被破解的)项目具有不同的背景。我正在使用 CHOICE_MODE_SINGLE。这是我的代码到目前为止的样子:


我使用的 ListView:
(在 layout.xml 内)

<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:listSelector="@drawable/selector_test" >
</ListView>

我在适配器中使用的 TextView 布局:
(listItem.xml)

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="23sp"
    android:textStyle="bold" />

这是我添加适配器的地方:

mListAdapter = new ArrayAdapter<String>(this, R.layout.listItem, mList);
mListView = (ListView) findViewById(R.id.listView);
mListView.setAdapter(mAuthorAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        String selected = mList.get(position);
        // ...
        mListView.setItemChecked(position, true);
    }
});

我确信点击时会检查正确的项目,因为当我调用 getCheckedItemPosition() 时,它会返回正确的值。


现在,为了突出选中的项目,我尝试了两件事:


选择器可绘制:
(selector_test.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="@android:integer/config_longAnimTime">

    <item android:state_checked="true"><shape>
            <solid android:color="@color/red" />
        </shape></item>
    <item android:state_selected="true"><shape>
            <solid android:color="@color/blue" />
        </shape></item>
    <item android:state_pressed="true"><shape>
            <solid android:color="@color/green" />
        </shape></item>

</selector>

我将它添加到 .xml 中:

android:listSelector="@drawable/selector_test"

背景可绘制:
(background_test.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/red" android:state_checked="true"/>
    <item android:drawable="@color/blue" android:state_selected="true"/>
    <item android:drawable="@color/green"/>

</selector>

我将它添加到 .xml 中:

android:background="@drawable/background_test"

我尝试将选择器和背景添加到 listView.xml 和 listItem.xml,但唯一改变的是默认背景颜色,以及当项目被按下(或按住)时选择器的颜色.
android:state_checked="true" 和 android:state_selected="true" 似乎什么也没做。

如果选择了视图,我可以通过覆盖 ArrayAdapter 中的 getView() 方法并在其中调用 setBackgroundColor() 来更改背景,它确实会更改背景,但也完全摆脱了选择器。此外,我真的不喜欢仅仅为了更改一行代码而重写类,特别是如果可以以不同的方式实现相同的事情。

所以我要问的是,有没有办法通过添加选择器或背景可绘制来突出显示 ListView 中选中的项目,我只是做错了,或者我必须以其他方式使其工作。

提前致谢! :)

【问题讨论】:

    标签: android xml listview android-listview listadapter


    【解决方案1】:

    在活动的 onStart 中添加这一行

    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    

    其中 lv 是 listView 的实例

    然后重写此方法并添加以下行。

     @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    
    
        // Set the item as checked to be highlighted 
        lv.setItemChecked(position, true);
                v.setBackgroundColor(Color.BLUE);
    
            conversationAdapter.notifyDataSetChanged();
    
    }
    

    然后在自定义适配器的 getView 方法中将先前选定项目的背景颜色更改回正常

    【讨论】:

    • 正如我所说,检查了正确的项目,只是它的背景没有改变。
    • 您好 Velja,我对我的回答做了一些更改,希望对您有所帮助。
    • 什么是正常的背景颜色? @RavinderBhandari 我不能给那个方法一个空值。
    【解决方案2】:

    试试这个:

    listViewDay.setItemChecked(position, true);
    listViewDay.setSelection(position);
    

    listitem.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:background="@drawable/list_day_selector" >
    
        <TextView
            android:id="@+id/txtItemDay"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_vertical|center_horizontal"
            android:text="22"
            android:textColor="@android:color/white"
            android:textSize="22sp" />
    
    </LinearLayout>
    

    list_selector.xml

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

    【讨论】:

    • 激活状态给了我错误提示....是不是少了什么东西? @JackDuong
    • 我得到的错误在这里:“这个项目是不可访问的,因为前一个项目(项目#1)是一个比这个更普遍的匹配”
    • 我想你想调用2.XML文件list_day_selector.xml。
    【解决方案3】:

    以编程方式使用 setSelector。例如:

    lv.setSelector(R.color.abc_background_cache_hint_selector_material_dark);
    

    【讨论】:

    • 这是大多数人搜索的内容
    【解决方案4】:

    在遇到一个接一个的故障几个小时后(包括选择器留下脏条纹),我决定自己做:

    class MyListAdapter extends ArrayAdapter<MyData> {
        // !!! access to the current selection
        int mSelected = 0;
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v =  super.getView(position, convertView, parent);
            // !!! this is where the selected item is highlighted
            v.findViewById(R.id.selectionSign).setVisibility(position == mSelected ? View.VISIBLE : View.INVISIBLE);
            return v;
        }
        public CommandListAdapter(Context context, List<MyData> objects) {
            super(context, R.layout.mylistitem, R.id.mytext, objects);
        }
    }
    

    使用 OnItemClickListener:

            mMyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    setSelectionPosNoUpd(position);
                    update();
                }
            });
    

    以及函数定义为:

        void setSelectionPosNoUpd(int n) {
            mCommandListAdapter.mSelected = n;
            //update();
        }
        void update() {
            mListViewOfCommands.smoothScrollToPosition(getSelectionPos());
            mCommandListAdapter.notifyDataSetChanged();
        }
    

    列表项 XML (res/layout/mylistitem.xml) 是:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/mytext" 
            />
        </LinearLayout>
        <View
            android:id="@+id/selectionSign"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#10ff5000"
            android:visibility="invisible"
            />
    </FrameLayout>
    

    缺点是更改选择后我必须update():没有简单的方法可以取消突出显示先前突出显示的视图。

    【讨论】:

      【解决方案5】:

      像这样在选择器中设置项目状态:

      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:state_activated="true"
              android:color="@color/XXX/>
      </selector>

      我不知道为什么 state_checked 不起作用。起初我认为它必须是 Checkableview 然后我尝试了 CheckedTextView。还是不行。
      无论如何,state_activated 会解决问题的。

      【讨论】:

        【解决方案6】:

        我认为你不能在匿名类中使用 mListView。您必须使用 AdapterView arg0

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        
                @Override
                public void onItemClick(AdapterView<?> arg0, View view,
                        int position, long id) {
                    AnyObject obj=(AnyObject)arg0.getItemAtPosition(position);
        
                                    ........
                                    .........
                                    .........
        
                }
            });
        

        【讨论】:

          【解决方案7】:

          这篇文章对我有用:http://www.mynewsfeed.x10.mx/articles/index.php?id=14

          ListView(在 layout.xml 中):

          <ListView
              android:id="@+id/list"
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:background="@drawable/list_custom"
              android:choiceMode="singleChoice"/>
          

          Selector drawable(list_custom.xml):

          <selector xmlns:android="http://schemas.android.com/apk/res/android">
              <item android:state_pressed="true" android:drawable="@color/orange"/>
              <item android:state_activated="true" android:drawable="@color/blue" />
              <item android:drawable="@color/green" />
          </selector>
          

          values 文件夹中的 Color.xml:

          <resources>
              <item name="blue" type="color">#FF33B5E5</item>
              <item name="green" type="color">#FF99CC00</item>
              <item name="orange" type="color">#FFFFBB33</item>
          </resources>
          

          【讨论】:

            猜你喜欢
            • 2011-08-16
            • 2012-06-23
            • 2013-04-17
            • 1970-01-01
            • 2013-11-18
            • 2012-01-05
            • 1970-01-01
            • 1970-01-01
            • 2013-02-04
            相关资源
            最近更新 更多