天天看点

NestedScrollView

NestedScrollView 初体验

出现的原因: 一般情况下,scrollview的内部或者外部无法添加另一个scrollview

“   It (  NestedScrollView  ) can be used as both parent or child   ScrollView  .  ”——网络博客的解释

“NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.”——官方介绍

示例代码:

<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="wrap_content"
                  android:orientation="vertical">
 
        <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:padding="@dimen/default_padding"
                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:textStyle="bold"/>
 
        </android.support.v4.widget.NestedScrollView>
 
        <include layout="@layout/cards_layout"/>
 
    </LinearLayout>
</ScrollView>                

得到的效果是: NestedScrollView内部的TextView如果太长,TextView内部的文字可以上下滑动查看。 

与此同时,整个LinearLayout也是可以滑动的(手指按住cards_layout上下滑动)

其他注意点: 正如普通的scrollview一样: 

  NestedScrollView可以使它的内部view拥有滑动效果 

  NestedScrollView  只能接受一个子view(通常是LinearLayout)