想要使用ScrollView來實現界面滾動,但是由於列表複雜,又要使用ListView加載數據,那麼就要實現嵌套。
只需要繼承ListView並重寫以下方法:
// 該自定義控件只是重寫了ListView的onMeasure方法,使其不會出現滾動條。
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
然後使用如下嵌套方式即可:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.demo.list.view.MyListView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</ScrollView>