Scrollview里面嵌套了一个Listview,我们一般会自定义ListView,重写onMeasure()方法来解决滑动冲突和显示问题.
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);
}
}
但是初始时页面不是置顶显示,而是显示到ListView展示数据的位置.
一般解决方法如下
1.在数据加载完成后将ScrollView控件置顶
scroll_view.smoothScrollTo(0, 0);
关于scrollTo()和scrollBy()方法的相关知识和理解建议参考这篇文章
Android Scroller完全解析,关于Scroller你所需知道的一切
2.在ScrollView的跟布局设置
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"