当前位置: 移动技术网 > IT编程>移动开发>Android > Android中listview嵌套scrollveiw冲突的解决方法

Android中listview嵌套scrollveiw冲突的解决方法

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

谁有黄网站啊,免费小说全文阅读,任丘老地方

一.使用网上用的动态改变listview高度的方法

该方法只适用于item布局是linearlayout布局的情况,不能是其他的,因为其他的layout(如relativelayout)没有重写onmeasure(),所以会在onmeasure()时抛出异常。所以使用限制较大。

public class utility { 
public static void setlistviewheightbasedonchildren(listview listview) { 
//获取listview对应的adapter 
listadapter listadapter = listview.getadapter();  
if (listadapter == null) { 
// pre-condition 
return; 
} 
 
int totalheight = 0; 
for (int i = 0, len = listadapter.getcount(); i < len; i++) { //listadapter.getcount()返回数据项的数目 
view listitem = listadapter.getview(i, null, listview); 
listitem.measure(0, 0); //计算子项view 的宽高 
totalheight += listitem.getmeasuredheight(); //统计所有子项的总高度 
} 
 
viewgroup.layoutparams params = listview.getlayoutparams(); 
params.height = totalheight + (listview.getdividerheight() * (listadapter.getcount() - 1)); 
//listview.getdividerheight()获取子项间分隔符占用的高度 
//params.height最后得到整个listview完整显示需要的高度 
listview.setlayoutparams(params); 
} 
} 

二.网上有帖子说在scrollview中添加一属性 android:fillviewport="true" ,这样就可以让listview全屏显示了。在我机器上测试失败了。

三.重写listview、gridview(推荐)

重写listview:

public class mylistview extends listview { 
 
  public mylistview(context context) { 
    // todo auto-generated method stub 
    super(context); 
  } 
 
  public mylistview(context context, attributeset attrs) { 
    // todo auto-generated method stub 
    super(context, attrs); 
  } 
 
  public mylistview(context context, attributeset attrs, int defstyle) { 
    // todo auto-generated method stub 
    super(context, attrs, defstyle); 
  } 
 
  @override 
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { 
    // todo auto-generated method stub 
    int expandspec = measurespec.makemeasurespec(integer.max_value >> 2, 
        measurespec.at_most); 
    super.onmeasure(widthmeasurespec, expandspec); 
  } 
} 

重写gridview:

/** 
 *自定义gridview,解决scrollview中嵌套gridview显示不正常的问题(1行) 
 */ 
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);  
    }  
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

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

相关文章:

验证码:
移动技术网