大家在开发过程中,肯定会用到ScrollView嵌套ListView或者GridView,我们都知道,这三个空间都有滑动的特性,所以嵌套的时候就会出现问题,一般是ListView或者GridView只显示第一行吧。其实这个问题很容易解决的。接下来我就以ListView为例,GridView是一样的。
核心代码如下:
我们需要自定义ListView
public class MyListView extends ListView{
public MyListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
在自定义的ListView中重写OnMeasure()方法即可,设置ListView显示的高度为Integer.MAX_VALUE>>2即可。
然后把自定义的ListView当做ListView用就可以了。如果还有问题,请加qq群137149792进行讨论,群里会有人给与答复。如果觉得有用,请点个赞,谢谢。