【Android】ListView与Button的共存问题解决
上一篇 /
下一篇 2012-12-19 10:53:04
/ 个人分类:android
ListView 和 其它能触发点击事件的widget无法一起正常工作的原因是加入其它widget后,ListView的itemclick事件将无法触发,被其它widget的click事件屏蔽。
我使用的是android2.3.3
1、activity_all_shop_list.xml 的配置文件
<?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" > <!--下面的ListView 配置是必须的,否则会报错--> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="false" /> </LinearLayout> |
2、shop_list_elem.xml:这个是list的配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- 店铺列表 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="50dp" android:orientation="horizontal" android:descendantFocusability="blocksDescendants" >
<ImageView android:id="@+id/shopElemImg" android:layout_width="80dp" android:layout_height="80dp" android:contentDescription="@string/empty_string" />
<TextView android:id="@+id/shopElemTitle" android:layout_width="wrap_content" android:layout_height="30dp" android:gravity="center_vertical" android:textSize="15sp" /> <Button android:id="@+id/favoriteShop" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center_vertical" android:textSize="15sp" android:focusable="false" android:onClick="favoriteShop" />
</LinearLayout> |
以上标红的部分一定要配置,
这两项的设置很关键,如果不设置,将导致ListView的ItemClick事件将无法触发,该事件被Button的click事件屏蔽了。
其中android:onClick="favoriteShop"为button的click触发的事件
3、对应的activity代码
@Override protected void onListItemClick(ListView l, View v, int position, long id) { //这里是list的单击事件,其中参数可以通过View.setTag View.getTag 来传递 } public void favoriteShop(View v){ //这里是button的click事件,其中参数可以通过View.setTag View.getTag 来传递 } |
收藏
举报
TAG: