scrollview嵌套gridview滑动问题

在开发过程总遇到ScrollView嵌套GridView,由于这两种控件都带有滚动条,当他们碰到一起的时候便会出问题,问题是gridview不滚动,并且只显示两行,为此看了官方文档,谷歌回答滚动里面没必要再加滚动,不符合UI设计。最后还是找到了网上大牛的解决方案才搞定的。

  大概写个demo测试了下,还是能嵌套使用的,提前GridView性能好像降低了。如果加载过多,UI加载变的很卡。

  主要xml布局为:

  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"   
  5.     android:scrollbars="none"  
  6.     >  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:background="#ff00ff"  
  12.         android:orientation="vertical" >  
  13.   
  14.         <com.test.MyGridView  
  15.             android:id="@+id/gridview"  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="wrap_content"  
  18.             android:background="#00ffff"  
  19.             android:numColumns="5" />  
  20.   
  21.         <LinearLayout  
  22.             android:layout_width="fill_parent"  
  23.             android:layout_height="1000dp"  
  24.             android:background="#ffff00" >  
  25.         </LinearLayout>  
  26.     </LinearLayout>  
  27.   
  28. </ScrollView></span>  

   里面的MyGridView继承了GridView重写了onMeasure方法,代码: 

  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.test;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.GridView;  
  6.   
  7. public class MyGridView extends GridView {   
  8.   
  9.     public MyGridView(Context context, AttributeSet attrs) {   
  10.         super(context, attrs);   
  11.     }   
  12.   
  13.     public MyGridView(Context context) {   
  14.         super(context);   
  15.     }   
  16.   
  17.     public MyGridView(Context context, AttributeSet attrs, int defStyle) {   
  18.         super(context, attrs, defStyle);   
  19.     }   
  20. //该自定义控件只是重写了GridView的onMeasure方法,使其不会出现滚动条,ScrollView嵌套ListView也是同样的道理,不再赘述。   
  21.     @Override   
  22.     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {   
  23.         int expandSpec = MeasureSpec.makeMeasureSpec(   
  24.                 Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);   
  25.         super.onMeasure(widthMeasureSpec, expandSpec);   
  26.     }   
  27. } </span>  

    通过上面重写的GridView,既可以嵌套到ScrollView里面。

原文地址:https://www.cnblogs.com/lucky-star-star/p/4063393.html