Android學習之——如何將GridView內嵌在ScrollView中

來源:互聯網
上載者:User

標籤:

      最近在做一個項目,有一個需求是在ScrollView中內嵌一個GridView。

      剛開始,我是以為能直接內嵌在裡面:

 1 <ScrollView 2         android:layout_width="match_parent" 3         android:layout_height="0dp" 4         android:layout_weight="5.5"> 5         <GridView 6             android:id="@+id/gridView" 7             android:layout_width="fill_parent" 8             android:layout_height="fill_parent" 9             android:numColumns="auto_fit"10             android:columnWidth="90dp"11             android:gravity="center"12             android:listSelector="@drawable/base_item_selector"13             android:stretchMode="columnWidth"14             ></GridView>15       </ScrollView>16     <RelativeLayout

 

 

1 mGridView = (GridView)findViewById(R.id.gridView);2           ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,3                   letters);4           mGridView.setAdapter(adapter);

 

   運行效果:

           只顯示一行。

                  

     分析原因:ScrollView和GridView都帶捲軸,引起衝突,導致GridView只能顯示一行。

    這就需要自訂GirdView:

  

 1 public class MyGridView extends GridView { 2     public MyGridView(Context context, AttributeSet attrs) { 3         super(context, attrs); 4     } 5     public MyGridView(Context context) { 6         super(context); 7     } 8     public MyGridView(Context context, AttributeSet attrs, int defStyle) { 9         super(context, attrs, defStyle);10     }11     @Override12     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {13         int expandSpec = MeasureSpec.makeMeasureSpec(14                 Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);15         super.onMeasure(widthMeasureSpec, expandSpec);16     }17 }
 1 <ScrollView 2         android:layout_width="match_parent" 3         android:layout_height="0dp" 4         android:layout_weight="5.5"> 5         <com.johntsai.view.MyGridView 6             android:id="@+id/gridView" 7             android:layout_width="fill_parent" 8             android:layout_height="fill_parent" 9             android:numColumns="auto_fit"10             android:columnWidth="90dp"11             android:gravity="center"12             android:listSelector="@drawable/base_item_selector"13             android:stretchMode="columnWidth"14             ></com.johntsai.view.MyGridView>15       </ScrollView>16     <RelativeLayout

運行效果:

   完美解決。

  

 

Android學習之——如何將GridView內嵌在ScrollView中

聯繫我們

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