天天看點

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)