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中有一些很有用的方法,可以禁用嵌套滚动、检查是否嵌套滚动父组件等等,具体可查看官方文档。