Android-ListView中嵌套(ListView)控件时的问题
当ListView中嵌套了一个Button或者ImageButton时,会让ListView的OnItemClickListener失去效果。本意是想让ListView中嵌套一个ListView(ListView嵌套ListView),呵呵。结果父View的OnItemClick事件不触发了。郁闷,调查了一下,找到一个解决方案。
1. 在子ListView的XML配置中,最顶层的Layout中增加属性:android:descendantFocusability="blocksDescendants"
2. 设置ListView的setFocusable为false就行了。
public StatisticsForcastListView(Context context,int period) { super(context,null); this.period = period; initialComponent(); } private void initialComponent() { //默认的列表背景色 this.setBackgroundColor(Constants.C_Color_Content_Background); //自动消失的滚动条 this.setScrollbarFadingEnabled(true); //不能点击 this.setClickable(false); //不能获取焦点 this.setFocusable(false); //设置高度为固定的值 layout.height = ScreenAdapter.getInstance().ComputeHeight(200); this.setLayoutParams(layout); //设置数据过滤器 if(adapter==null) { adapter = new StatisticsForcastDataAdapter(getContext(),period); } this.setAdapter(adapter); }
Adapter对应的Xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:orientation="horizontal" android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants" ><!-- 主窗体 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="5.5" android: android:gravity="center" android:text="12,15,2,4,6,9,14" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3.8" android: android:gravity="center" android:text="2011-08-25 12:30:27" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3.8" android: android:gravity="center" android:text="未中奖" /> </LinearLayout>
最后更新:2017-04-02 17:51:23