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