在开发过程总遇到ScrollView嵌套GridView,由于这两种控件都带有滚动条,当他们碰到一起的时候便会出问题,问题是gridview不滚动,并且只显示两行,为此看了官方文档,谷歌回答滚动里面没必要再加滚动,不符合UI设计。最后还是找到了网上大牛的解决方案才搞定的。
大概写个demo测试了下,还是能嵌套使用的,提前GridView性能好像降低了。如果加载过多,UI加载变的很卡。
主要xml布局为:
- <span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
 - <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - android:scrollbars="none"
 - >
 - <LinearLayout
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:background="#ff00ff"
 - android:orientation="vertical" >
 - <com.test.MyGridView
 - android:id="@+id/gridview"
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:background="#00ffff"
 - android:numColumns="5" />
 - <LinearLayout
 - android:layout_width="fill_parent"
 - android:layout_height="1000dp"
 - android:background="#ffff00" >
 - </LinearLayout>
 - </LinearLayout>
 - </ScrollView></span>
 
里面的MyGridView继承了GridView重写了onMeasure方法,代码:
- <span style="font-family:KaiTi_GB2312;font-size:18px;">package com.test;
 - import android.content.Context;
 - import android.util.AttributeSet;
 - import android.widget.GridView;
 - public class MyGridView extends GridView {
 - public MyGridView(Context context, AttributeSet attrs) {
 - super(context, attrs);
 - }
 - public MyGridView(Context context) {
 - super(context);
 - }
 - public MyGridView(Context context, AttributeSet attrs, int defStyle) {
 - super(context, attrs, defStyle);
 - }
 - //该自定义控件只是重写了GridView的onMeasure方法,使其不会出现滚动条,ScrollView嵌套ListView也是同样的道理,不再赘述。
 - @Override
 - public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 - int expandSpec = MeasureSpec.makeMeasureSpec(
 - Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
 - super.onMeasure(widthMeasureSpec, expandSpec);
 - }
 - } </span>
 
通过上面重写的GridView,既可以嵌套到ScrollView里面。
来源:https://www.cnblogs.com/lucky-star-star/p/4063393.html