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

所属分类: 软件编程 / Android 阅读数: 31
收藏 0 赞 0 分享

一.使用网上用的动态改变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);  
    }  
} 

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

更多精彩内容其他人还在看

android byte[] 和short[]转换的方法代码

这篇文章主要介绍了android byte[] 和short[]转换的方法代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取应用程序大小的方法

这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取其他包的Context实例代码

这篇文章主要介绍了Android获取其他包的Context实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android放大镜的实现代码

这篇文章主要介绍了Android放大镜的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android 读取Properties配置文件的小例子

这篇文章主要介绍了Android 读取Properties配置文件的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android通讯录开发之删除功能的实现方法

这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享
查看更多