最近做項目,用到了listview的item的一些問題,現在抽空把它們總結一下:
轉載請表明出處:http://blog.csdn.net/wdaming1986/article/details/6788751
1、如果listview的item中有button,checkbox的時候,往往如果不做處理的情況,button,checkbox會獲得焦 點,導致listview不相應OnItemLongClickListener()這個方法,搶佔了Item的獲得焦點的能力,解決的辦法有兩種:
(1)在button,checkbox的xml布局檔案中添加代碼:
android:focusable=“false”
即就可以相應OnItemLongClickListener()這個方法了。
(2)在listview中的item的布局檔案中,在總的LinearLayout的屬性中添加一句話:
android:descendantFocusability="blocksDescendants"
即把在LinearLayout裡面的focusable屬性都設定成了false,
就可以相應OnItemLongClickListener()這個方法了。
2、在ListView的item中有GridView,搶佔焦點的情況,解決辦法步驟如下:
(1) 現在ListView的Item的最外面的LinearLayout中設定:、
android:descendantFocusability="blacksDescendants"
(2) 再在gridview的getView()方法中設定:
gridview.setClickable(false);
gridview.setPressed(false);
gridview.setEnabled(false);
即就可以相應OnItemLongClickListener()這個方法了。
3、如果在布局檔案中有一個或多個GridView,如果在item的布局中加入了ScrollView,如果不對gridview的item設定高度的話,這個滾動的效果只是在GridView中體現,而不在整個listview布局中體現,要想在整個布局中有上下滾動的效果,就需要設定gridview中的item的高度進行設定。具體代碼可以參考下面的:
gv.setNumColumns(7);fra = new FilterResultAdapter(mainActivity,mainActivity,AL);//動態設定GridView的高度LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) gv.getLayoutParams(); // 取控制項mGrid當前的布局參數if(fra.getCount() % 7 != 0){linearParams.height = (Math.round(fra.getCount()/7 + 1))*34+20;// 設定高度}else{linearParams.height = (Math.round(fra.getCount()/7))*34+20;// 設定高度}gv.setLayoutParams(linearParams); // 使設定好的布局參數應用到控制項