ssummeraw 2012-11-30 06:37 采纳率: 10%
浏览 9128
已采纳

Listview 中的ListSelector不能正常运行

我自定义了 listselector方法:

<?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/bg_list_hover"></item>
    <item 
        android:state_focused="true"
        android:drawable="@drawable/bg_list"></item>
</selector>

在Listview中是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bg_dasar" >
    <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="StoreCategory" 
                android:textSize="15sp"
                android:paddingLeft="10dip"
                android:paddingTop="10dip"
                android:paddingRight="10dip"
                android:textColor="@color/textcolor"/>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:listSelector="@drawable/listselector">
    </ListView>

我的疑惑是当我不点击listview视图时,列表视图项就不会使用focused state的背景(@drawable/bg_list)。
请求大家的指点,谢谢!

  • 写回答

3条回答

  • nana4ever 2012-11-30 09:43
    关注

    在listselector 改成这样:

    <?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/bg_list_hover" />
     <item 
         android:drawable="@drawable/bg_list" />
    </selector>
    

    listview布局中:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/bg_dasar" >
        <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="StoreCategory" 
                    android:textSize="15sp"
                    android:paddingLeft="10dip"
                    android:paddingTop="10dip"
                    android:paddingRight="10dip"
                    android:textColor="@color/textcolor"/>
    
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dip"
            **android:listSelector="@android:color/transparent"**>
        </ListView>
    
    </LinearLayout>
    

    把list selector添加到list item后台中:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="55dip"
        android:orientation="horizontal" 
        android:padding="15dip"
        android:gravity="center_vertical"
        **android:background="@drawable/listselector"**>
    
        <ImageView
            android:id="@+id/imageViewlogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dip" />
    
        <TextView
            android:id="@+id/textViewcategory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="@color/textcolor" 
            android:textSize="15sp"/>
    
    </LinearLayout>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题