ScrollView中ListView显示不全解决方法

来源:互联网 发布:淘宝网店服装头像图片 编辑:程序博客网 时间:2024/05/18 01:25
自定义一个类继承自ListView,通过重写其onMeasure方法,达到对ScrollView适配的效果。


下面是继承了ListView的自定义类:


[java] view plain copy
  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.widget.ListView;  
  4. public class ListViewForScrollView extends ListView {  
  5.     public ListViewForScrollView(Context context) {  
  6.         super(context);  
  7.     }  
  8.     public ListViewForScrollView(Context context, AttributeSet attrs) {  
  9.         super(context, attrs);  
  10.     }  
  11.     public ListViewForScrollView(Context context, AttributeSet attrs,  
  12.         int defStyle) {  
  13.         super(context, attrs, defStyle);  
  14.     }  
  15.     @Override  
  16.     /** 
  17.      * 重写该方法,达到使ListView适应ScrollView的效果 
  18.      */  
  19.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  20.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
  21.         MeasureSpec.AT_MOST);  
  22.         super.onMeasure(widthMeasureSpec, expandSpec);  
  23.     }  
  24. }  

三个构造方法完全不用动,只要重写onMeasure方法,需要改动的地方比较少。


在xml布局中和Activty中使用的ListView改成这个自定义ListView就行了。

这个方法有一个同样的毛病,就是默认显示的首项是ListView,需要手动把ScrollView滚动至最顶端。


  • sv = (ScrollView) findViewById(R.id.act_solution_4_sv);
  • sv.smoothScrollTo(0, 0);

0 0
原创粉丝点击