- 自定义ListView 重写其onMeasure(),方法
public class ListViewForScrollView extends ListView{
public ListViewForScrollView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public ListViewForScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ListViewForScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
2.布局文件引用
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/act_sv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="\n上方数据\n" />
<com.crazyandroid.custom.ListViewForScrollView
android:id="@+id/act_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</com.crazyandroid.custom.ListViewForScrollView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="\n下方数据\n" />
</LinearLayout>
</ScrollView>
3.java代码
mScrollView = (ScrollView) findViewById(R.id.act_sv);
mScrollView.smoothScrollTo(0, 0);//确保ScrollView从顶端显示
4.更多解决方法 请参阅:
http://www.apkbus.com/forum.php?mod=viewthread&tid=161576