【问题标题】:How to mark a listview item?如何标记列表视图项?
【发布时间】:2013-09-19 18:35:44
【问题描述】:

我知道这个问题已经在questionthis 这样的问题中讨论过很多次。 但我不得不说它对我不起作用。

我想要实现的是在特殊情况下通过我的 CustomAdapter 标记一个 ListView 项。所以,不能通过用户和点击监听器。我有一个可行的解决方案,方法是为 convertView 设置一个可绘制的背景。

现在我想改变这一点,让选择器完成这项工作,这是更清洁的解决方案。但我无法让它工作。设置按下项目的行为并设置不同的颜色没有问题。 但是我在按下项目后无法将其标记为选中。

我用我的 listview_item_selector 和 listview_selector 尝试了不同的组合。而且我认为,我错过了一些非常普遍的东西。这就是为什么我进行设置只是为了弄清楚如何在按下项目后对其进行标记。

这是我的 list_item_selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@android:color/transparent"/>
    <item android:state_selected="true" android:drawable="@android:color/transparent"/>
    <item android:drawable="@drawable/state_normal"/>
</selector>

而我的 listview_selector 设置为 ListView 作为选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/state_pressed"></item>
    <item android:state_selected="true" android:drawable="@drawable/selected_item"/>   
</selector>

正如我所说,state_pressed 正在工作。但是state_selected 让我抓狂。

至少我的自定义项目布局:

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

    <TextView 
        android:id="@+id/list_tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFFFEE"/>

    <TextView 
        android:id="@+id/list_tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFEEEE"/>
</LinearLayout>

如果有任何建议,我将不胜感激。

【问题讨论】:

    标签: android android-layout listview custom-adapter android-selector


    【解决方案1】:

    因为您缺少一个状态,您需要将该行标记为已激活,并具有指示该行已为激活状态选中的可绘制对象

    查看我的问题以获取示例

    Showing the current selection in a listview

    然后使用

    listview.setItemChecked(position,true);
    

    【讨论】:

    • 非常感谢!我不知道那个状态,我必须设置激活背景指示器。拯救了我的一天
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2011-08-02
    • 2014-11-15
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多