【问题标题】:Android Listview Selected Item color must remain changed even if finger is lifted up即使手指抬起,Android Listview Selected Item 颜色也必须保持更改
【发布时间】:2012-10-10 18:56:56
【问题描述】:

我关注了很多这样的问题 1 change color of selected listview item,但没有发现任何有用的问题。

我有一个列表视图,我想要的是,所选项目必须突出显示,例如蓝色,即使在用户抬起手指后也是如此。

这是我在drawable中的row_color.xml

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

    <item android:state_pressed="false" android:state_focused="false"
        android:drawable="@android:color/transparent" />

    <item android:state_pressed="true" android:drawable="@color/light_sea_green" />

    <item android:state_selected="true" android:drawable="@color/light_sea_green" />

</selector>

这是我正在膨胀的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:background="@drawable/row_color">

    <ImageView
        android:id="@+id/imageView_game_row_face"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/af025501" />

</LinearLayout>

【问题讨论】:

    标签: android listview layout


    【解决方案1】:

    将这个添加到你的选择器

    <item android:drawable="@android:color/transparent" />
    

    这将是您的默认可绘制对象。

    你可以删除

     <item android:state_pressed="false" android:state_focused="false"
            android:drawable="@android:color/transparent" />
    

    更新->

    这是一个可以帮助你的代码 sn-p -

    public View getView(int position, View convertView, ViewGroup parent) {
    
    /* Your code reusing code here */
    
        if(position == selectedItemPosition which you must save in onItemClickListener of the listview)
            convertView.setBackgroundResource(selecteditem background color);
        }
        else {
            convertView.setBackgroundResource(normal background color);
        }
    
    }
    

    else 部分非常重要。否则会导致回收的视图带有相同的背景颜色。

    【讨论】:

    • 我按照你的建议做了,但它甚至没有显示选择器颜色,我的问题仍未解决
    • 您将颜色设置为 row_color,而选择器文件名为 row.xml。那么这是复制粘贴错误吗?
    • 抱歉,是的,是复制粘贴错误,我已经编辑了我的问题
    • 在你的 color.xml 中创建这个透明颜色(#00000000) 并在这里使用它。看看有没有帮助。
    • 主要问题是,标签 .... ,在不工作的情况下,即使 state_pressed 工作,其余代码也在工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    相关资源
    最近更新 更多