【问题标题】:How to highlight selected list item in android?如何在android中突出显示选定的列表项?
【发布时间】:2013-02-01 11:26:36
【问题描述】:

在我的 android 应用程序中,我有每个列表项的列表视图和详细视图。对于平板电脑,我显示了项目的列表视图和所选项目的详细视图,如下所示。

所以我的问题是如何在用户单击列表项后突出显示所选项目。

我正在使用 BaseAdapter 加载列表视图。我该怎么做??

编辑:

是的,正如 chintan khetiya 提到的,我使用以下 xml 文件作为列表项的背景,但它不会使所选项目感到高兴。我错过了什么?

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

【问题讨论】:

    标签: android android-listview android-fragments android-ui


    【解决方案1】:

    您的查询:

    我的问题是如何在用户单击列表项后突出显示所选项目。

    我想你是在问选择器。意味着如果列表行处于焦点状态,那么它应该与所有其他行看起来不同。按下或触摸行时也是如此。

    为此,您必须在 Drawable 文件夹中创建 Selector.xml 文件,然后将该选择器文件放在列表行中

    该文件应该有不同的标签,如Focus-Click-Press,并根据状态更改 Drawable。

    更新:

    只需替换您的图标并保存在 Drawable 文件夹中。

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- Pressed -->
        <item android:drawable="@drawable/p_paly_press" android:state_pressed="true"/>
    
        <!-- selected -->
        <item android:drawable="@drawable/p_play" android:state_selected="true"/>
    
        <!-- focused -->
        <item android:drawable="@drawable/p_paly_press" android:state_focused="true"/>
    
        <!-- default -->
        <item android:drawable="@drawable/p_play"/>
    
    </selector>
    

    【讨论】:

    • 是的 chintan khetiya 我已经编辑了问题,你能告诉我该代码的错误部分吗?
    • 它对我有用,我在这里粘贴了相同的代码。其他问题
    【解决方案2】:

    如果其他方式不需要,可以使用类似于以下代码的代码来突出显示选定的项目:

    class Adapter extends ArrayAdapter<String> {
    
        private int selectedPos = -1;
        Drawable selectedBackground;
    
        public MainSelectAdapter(Context context, int textViewResourceId,
                List<String> objects) {
            super(context, textViewResourceId, objects);
            selectedBackground =
                  context.getResources().getDrawable(R.color.selecteditembackground);
        }
        public void setSelectedPosition(int pos){
            selectedPos = pos;
            notifyDataSetChanged();
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            if (selectedPos == position) {
                v.setBackgroundDrawable(selectedBackground);
            } else {
                v.setBackgroundDrawable(null);
            }
            return v;
        }
    }
    

    在主要活动中

      Adapter adapter = new Adapter(this, android.R.layout.simple_list_item_1,
        myItemsToShow);
    
      list = (ListView) findViewById(R.id.flows);
      list.setItemsCanFocus(true);
      list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
      list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapter, View view,
                    int pos, long id) {
                    adapter.setSelectedPosition(pos);
        }
      });
    

    使用这种方法,您可以自行控制所选项目的突出显示,您有一个侦听器来捕获选择事件,并为自己设置所需的 Drawable。

    【讨论】:

    • 这种方法适用于ArrayAdapter,但如果有人使用CursorAdapter怎么办?
    【解决方案3】:

    你可以在你的styles.xml中定义

        <style name="Theme.Base" parent="...">
            <item name="activatableItemBackground">@drawable/activatable_item_background</item>
        </style>
    
       <style name="ListItemContainerBase">
            <item name="android:background">?activatableItemBackground</item>
        </style>
    

    在 res/drawable 中定义 activatable_item_background.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/item_pressed" android:state_pressed="true" />
        <item android:drawable="@drawable/item_focused" android:state_focused="true" />
        <item android:drawable="@drawable/item_focused" android:state_selected="true" />
        <item android:drawable="@drawable/item_activated" android:state_activated="true" />
        <item android:drawable="@drawable/item_checked" android:state_checked="true" />
        <item android:drawable="@android:color/transparent" />
    </selector>
    

    item_pressed, item_focused..... 是 res/drawable-xxx 中的图片

    为视图中的每个项目定义如下布局:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/ListItemContainerBase">
    

    【讨论】:

      猜你喜欢
      • 2021-10-29
      • 2021-10-29
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 2018-11-10
      • 2012-06-24
      相关资源
      最近更新 更多