解决ScrollView嵌套ListView显示不全问题

来源:互联网 发布:python 读写文本文件 编辑:程序博客网 时间:2024/05/22 01:51

在页面布局中,我们有可能会碰到在ScrollView中显示一个LIstView的情况,但这两个控件都是带滚动条的,这样就会导致ListView只显示一条记录,而且不能获取滚动焦点。

这里写图片描述

要解决这个问题,我们可以自定义一个ListView,使其自适应ScrollView显示:

public class MyList extends ListView {     public MyList(Context context) {         super(context);     }     public MyList(Context context, AttributeSet attrs) {         super(context, attrs);     }     public MyList(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);     }     @Override     /**      * 重写该方法,达到使ListView适应ScrollView的效果      */     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);         super.onMeasure(widthMeasureSpec, expandSpec);     } } 

在资源布局文件中这样使用自定义的控件:

<com.myproject.ui.MyList    android:id="@+id/listView1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:paddingLeft="10dp" ></com.myproject.ui.MyList>

ok,至此,自定义的工作完成了,我们可以正常给ListView加adapter就好,ListView能够完全的显示全部条目,不会与ScrollView冲突了。

另,发现了网上还有多种方法可以解决这个问题,mark:
http://bbs.anzhuo.cn/thread-982250-1-1.html

0 0