当前位置: 移动技术网 > IT编程>移动开发>Android > Android中ScrollView嵌套GridView的解决办法

Android中ScrollView嵌套GridView的解决办法

2019年07月24日  | 移动技术网IT编程  | 我要评论

盛夏的果实伴奏,吉林省全城热恋,龙飞九宵

前些日子在开发中用到了需要scrollview嵌套gridview的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即gridview会显示不全。 找到大家的通用解决办法。记录一下。

解决办法,自定义一个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);  
  }  
  @override  
  public void onmeasure(int widthmeasurespec, int heightmeasurespec) {  
    int expandspec = measurespec.makemeasurespec(  
        integer.max_value >> 2, measurespec.at_most);  
    super.onmeasure(widthmeasurespec, expandspec);  
  }  
}  

该自定义控件只是重写了gridview的onmeasure方法,使其不会出现滚动条,scrollview嵌套listview也是同样的道理,不再赘述。

xml布局代码

<scrollview android:layout_height="wrap_content"  
    android:layout_width="fill_parent" android:id="@+id/scroll_content">  
    <com.yourclass.mygridview xmlns:android="http://schemas.android.com/apk/res/android"  
      android:id="@+id/grid_view" android:layout_width="fill_parent"  
      android:layout_height="wrap_content" android:numcolumns="auto_fit"  
      android:horizontalspacing="1dip" android:verticalspacing="1dip"  
      android:columnwidth="150dip" android:stretchmode="columnwidth"  
      android:gravity="center">  
        
    </com.yourclass.mygridview>  
  </scrollview>  

java调用代码

mygridview gridview = (mygridview) findviewbyid(r.id.grid_view);  
gridview.setadapter(new imageadapter()); 

以上所述是小编给大家介绍的android中scrollview嵌套gridview的解决办法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网