ListView Item 点击事件 被重写BaseAdapter里面子控件抢去问题

来源:互联网 发布:如何在淘宝开代购店 编辑:程序博客网 时间:2024/06/11 03:48
通常默认能获得focus的控件有Button,Checkable继承来的所有控件,这就意味着如果你的自定义ListViewItem中 有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取 Focus,所以item中的子控件的focus必须为false,这样item才能获取onItemClick事件,(RatingBar中有个 isIndicator属性表示RatingBar是否是指示用的,应该设为true,不然也获取不到焦点)

1. 将ListView的Item Layout的子控件focusable属性设置为false  
2. 对Item Layout的根控件android:descendantFocusability="blocksDescendant"  

listviewItem布局 例子:
<?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="wrap_content"
    android:orientation="horizontal" 
   
android:descendantFocusability="blocksDescendants"  
    android:background="@android:color/background_light"
    >
    <ImageView 
        android:focusable="false"
        android:layout_width="90dip"
    android:layout_height="90dip"
    android:id="@+id/file_icon"
    android:background="@drawable/pdf"
        />
    <TextView 
        android:focusable="false"
        android:layout_width="match_parent"
    android:layout_height="90dip"
    android:id="@+id/file_name"
    android:gravity="left|center"
    android:textSize="18sp"
        />
</LinearLayout>
原创粉丝点击