【问题标题】:Selector for Multi Choice ListView多选列表视图的选择器
【发布时间】:2014-09-26 05:55:48
【问题描述】:

我无法在 android 中使用自定义适配器突出显示多选列表视图。激活和按下状态工作得很好。我想要的是当用户点击一个项目时它应该被突出显示。

我的 new_list_selector.xml

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

在上面的 xml 中,“激活”和“按下”的状态是有效的,但不是“选择”。

在我的 listview_item.xml 中,我将背景设置为选择器,如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@drawable/new_list_selector">
<!--list items goes here-->
</RelativeLayout>

我这样定义我的列表视图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/masterContainer">
<ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:focusable="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/friendViewList" />
</RelativeLayout>

当我点击并按住一个列表项时,我会进入激活状态。当我点击列表项时,按下状态有效。我希望在用户点击该项目时突出显示它。有人可以帮我解决这个问题吗?感谢您提前提供任何意见。

PS:我更喜欢 xml 解决方案而不是编码。我也尝试过将state_focused 设置为true 的选择器,但没有成功

【问题讨论】:

  • 那就不要设置为layout的背景。而是将其作为其中存在的项目的背景。
  • 感谢您的意见。这确实是列表视图中显示的项目的布局。此布局包含一个图像和两个文本视图,为简单起见,我将其删除

标签: android xml listview layout selector


【解决方案1】:

执行以下操作:

listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp">
<ImageView 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/new_list_selector"/>

</RelativeLayout>

这将在 ImageView 上设置选择器。你也可以用你的两个 TexTviews 来做。

【讨论】:

  • 我一定会试试这个并回复你
  • 刚刚试了一下。不幸的是,这并没有解决我的问题
  • 你用 ImageView 试过了吗?您是否在动态覆盖 ImageView 的背景?
  • 是的,我尝试使用图像视图和文本视图。我不会在任何地方覆盖 imageview。我只是在我的自定义适配器中设置图像。
  • 它应该可以工作。上传您的活动截图。
猜你喜欢
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
相关资源
最近更新 更多