ScrollView的嵌套
在ScrollView中嵌套ScrollView虱颗,很多情況下可能出現問題。一般通過添加幾個不同的解決方案(如觸摸事件)來實現踱承,但此方法過于復雜且不易維護排作。
通過使用NestedScrollView則可以很方便地實現ScrollView的嵌套。
使用NestedScrollView
直接上示例代碼:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="@dimen/nested_scroll_view_height">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</ScrollView>
NestedScrollView中有一些很有用的方法寇僧,可以禁用嵌套滾動摊腋、檢查是否嵌套滾動父組件等等,具體可查看官方文檔嘁傀。