Android中ListView中有button,checkbox,GridView的用法【安卓進化二十五】

來源:互聯網
上載者:User

           最近做項目,用到了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); // 使設定好的布局參數應用到控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.