android 在ScrollView中嵌入GridView

來源:互聯網
上載者:User
android 在ScrollView中嵌入GridView
做android程式開發的都知道,不能在一個擁有Scrollbar的組件中嵌入另一個擁有Scrollbar的組件,因為這不科學,會混淆滑動事件,導致只顯示一到兩行資料。那麼就換一種思路,首先讓子控制項的內容全部顯示出來,禁用了它的滾動。如果超過了父控制項的範圍則顯示父控制項的scrollbar滾動顯示內容,思路是這樣,一下是代碼。

具體的方法是自訂GridView組件,繼承自GridView。重載onMeasure方法:

 

 
  1. public class MyGridView extends GridView
      
  2. {   
  3.     public MyGridView(android.content.Context context,   
  4.             android.util.AttributeSet attrs)   
  5.     {   
  6.         super(context, attrs);   
  7.     }   
  8.   
  9.     /**
     
  10.      * 設定不滾動  
  11.      */  
  12.     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
      
  13.     {   
  14.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
      
  15.                 MeasureSpec.AT_MOST);   
  16.         super.onMeasure(widthMeasureSpec, expandSpec);   
  17.   
  18.     }   
  19.   
  20. }  
public class MyGridView extends GridView{public MyGridView(android.content.Context context,android.util.AttributeSet attrs){super(context, attrs);}/** * 設定不滾動 */public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expandSpec);}}

其中onMeasure函數決定了組件顯示的高度與寬度;

makeMeasureSpec函數中第一個函數決定布局空間的大小,第二個參數是配置模式

MeasureSpec.AT_MOST的意思就是子控制項需要多大的控制項就擴充到多大的空間

 之後在ScrollView中添加這個組件就OK了,同樣的道理,ListView也適用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.