在SrollView中嵌套GridView或ListView
2015-09-23 11:27
411 查看
我想在同一个界面中,使用两个GridView,两个GridView一起上下滚动;
如果直接将两个GridView添加到同一个界面上,它们是各自滚动的。
因此,我考虑使用SrollView,将它们包装一下!
但这样做会提示如下信息:
The vertically scrolling ScrollView should not contain another vertically scrolling widget (GridView)
并且GridView的界面也显示不全,只显示了一部分。
网上搜了一下,使用下面这个方法,效果挺好的!能满足我的需求!
从GridView派生出一个自定义的类(MyGridView),重载其测量方法(onMeasure):
[java] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
/**
* @Type: MyGridView
* 让GridView可以做ScrollView的子控件,但尺寸不会减小
*/
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
在XML中,将原来使用系统的GridView,替换成自定义的MyGridView即可:
[html] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<com.gaojinshan.MyGridView
android:id="@+id/gv_app1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:horizontalSpacing="0dp"
android:gravity="center"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="0dip"
/>
<com.gaojinshan.MyGridView
android:id="@+id/gv_app2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/gv_app1"
android:horizontalSpacing="0dp"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="0dip"
android:scrollbars="none"
/>
</RelativeLayout>
</ScrollView>
在SrollView中,嵌套ListView,也可以同样处理,不再赘述!
版权声明:本文为博主原创文章,未经博主允许不得转载。
如果直接将两个GridView添加到同一个界面上,它们是各自滚动的。
因此,我考虑使用SrollView,将它们包装一下!
但这样做会提示如下信息:
The vertically scrolling ScrollView should not contain another vertically scrolling widget (GridView)
并且GridView的界面也显示不全,只显示了一部分。
网上搜了一下,使用下面这个方法,效果挺好的!能满足我的需求!
从GridView派生出一个自定义的类(MyGridView),重载其测量方法(onMeasure):
[java] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
/**
* @Type: MyGridView
* 让GridView可以做ScrollView的子控件,但尺寸不会减小
*/
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
在XML中,将原来使用系统的GridView,替换成自定义的MyGridView即可:
[html] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<com.gaojinshan.MyGridView
android:id="@+id/gv_app1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:horizontalSpacing="0dp"
android:gravity="center"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="0dip"
/>
<com.gaojinshan.MyGridView
android:id="@+id/gv_app2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/gv_app1"
android:horizontalSpacing="0dp"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="0dip"
android:scrollbars="none"
/>
</RelativeLayout>
</ScrollView>
在SrollView中,嵌套ListView,也可以同样处理,不再赘述!
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐
- Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式总结
- Apache 无法正常启动问题
- mysql中enum的用法
- 关于eclipse中MAVEN WEB工程中编译问题
- 栈的链表实现
- UIActivityIndicatorView的使用
- java 随机获取国内IP
- 多线程学习网站,比较详细
- windows安装docker
- 排序算法 面试 JAVA
- rsync命令
- opencv学习笔记(十)-图像腐蚀与腐蚀
- android二维码的生成与解析
- c语言学习笔记(4)struct和union:柔性数组存储斐波那契数列以及union判断系统大小端
- [进化算法] 随机Q-竞争选择法
- spring学习历程 之 事件(二)
- HTTP 错误 404.3 - Not Found(WCF+ASP.NET)
- sublime text 2 更新后emmet插件tab键无法使用的解决办法
- Unique path
- proftpd的示例配置文件