• 在SrollView中嵌套GridView或ListView(转)


    原文链接:http://blog.csdn.net/gaojinshan/article/details/17055511

      我想在同一个界面中,使用两个GridView,两个GridView一起上下滚动;
    如果直接将两个GridView添加到同一个界面上,它们是各自滚动的。
    因此,我考虑使用SrollView,将它们包装一下!
    但这样做会提示如下信息:
    The vertically scrolling ScrollView should not contain another vertically scrolling widget (GridView)
    并且GridView的界面也显示不全,只显示了一部分。

      网上搜了一下,使用下面这个方法,效果挺好的!能满足我的需求!
    从GridView派生出一个自定义的类(MyGridView),重载其测量方法(onMeasure):

     1 /** 
     2  * @Type: MyGridView 
     3  * 让GridView可以做ScrollView的子控件,但尺寸不会减小 
     4  */  
     5 public class MyGridView extends GridView {  
     6       
     7     public MyGridView(Context context) {    
     8         super(context);    
     9     }    
    10       
    11     public MyGridView(Context context, AttributeSet attrs) {    
    12         super(context, attrs);    
    13     }    
    14       
    15     public MyGridView(Context context, AttributeSet attrs, int defStyle) {    
    16         super(context, attrs, defStyle);    
    17     }    
    18     
    19     @Override    
    20     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    
    21     
    22         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,    
    23                 MeasureSpec.AT_MOST);    
    24         super.onMeasure(widthMeasureSpec, expandSpec);    
    25     }      
    26 }    

    在XML中,将原来使用系统的GridView,替换成自定义的MyGridView即可:

     1 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
     2     xmlns:tools="http://schemas.android.com/tools"  
     3     android:layout_width="fill_parent"  
     4     android:layout_height="wrap_content">  
     5 <RelativeLayout   
     6     android:layout_width="wrap_content"  
     7     android:layout_height="wrap_content"  
     8     tools:context=".MainActivity" >  
     9       
    10     <com.gaojinshan.MyGridView  
    11         android:id="@+id/gv_app1"  
    12         android:layout_width="wrap_content"  
    13         android:layout_height="wrap_content"  
    14         android:horizontalSpacing="0dp"  
    15         android:gravity="center"  
    16         android:numColumns="4"  
    17         android:stretchMode="columnWidth"   
    18         android:verticalSpacing="0dip"  
    19         />  
    20   
    21     <com.gaojinshan.MyGridView  
    22         android:id="@+id/gv_app2"  
    23         android:layout_width="wrap_content"  
    24         android:layout_height="wrap_content"  
    25         android:layout_below="@id/gv_app1"  
    26         android:horizontalSpacing="0dp"  
    27         android:gravity="center"  
    28         android:numColumns="2"  
    29         android:stretchMode="columnWidth"   
    30         android:verticalSpacing="0dip"  
    31         android:scrollbars="none"  
    32         />  
    33 </RelativeLayout>  
    34 </ScrollView>  
  • 相关阅读:
    VS2013 添加 ILDasm
    XmlIgnore的使用
    前端常见跨域解决方案(全)
    网络请求中的cookie与set-Cookie的交互模式的一些问题解析
    各浏览器Cookie大小、个数限制
    Linux常用命令分类
    给博客园中博文的图片添加单击时弹出放大效果——lightbox
    给博客园中博文的图片添加单击时弹出放大效果—— zoom.js
    CSS3新单位vw、vh、vmin、vmax使用详解
    session,cookie,sessionStorage,localStorage的相关设置以及获取删除
  • 原文地址:https://www.cnblogs.com/laishenghao/p/5154739.html
Copyright © 2020-2023  润新知