Android新特性:NestedScrollView的使用,替代ScrollView

来源:互联网 发布:淘宝买家卖家账户分开 编辑:程序博客网 时间:2024/06/07 01:21

 在新版的support-v4兼容包里面有一个NestedScrollView控件,这个控件其实和普通的ScrollView并没有多大的区别,这个控件其实是MD(Meterial Design)中设计的一个控件,目的是跟MD中的其他控件兼容。应该说在MD中,RecyclerView代替了ListView,而NestedScrollView代替了ScrollView,他们两个都可以用来跟ToolBar交互,实现上拉下滑中ToolBar的变化。在NestedScrollView的名字中其实就可以看出他的作用了,Nested是嵌套的意思,而ToolBar基本需要嵌套使用。

        xml的布局如下:

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/main_content"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true">    <!--第一部分:伸缩工具栏-->    <android.support.design.widget.AppBarLayout        android:id="@+id/appbar"        android:layout_width="match_parent"        android:layout_height="200dp"        android:fitsSystemWindows="true"        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">        <android.support.design.widget.CollapsingToolbarLayout            android:id="@+id/collapsing_toolbar"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:fitsSystemWindows="true"            app:contentScrim="?attr/colorPrimary"            app:expandedTitleMarginEnd="64dp"            app:expandedTitleMarginStart="48dp"            app:layout_scrollFlags="scroll|exitUntilCollapsed">            <ImageView                android:id="@+id/backdrop"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:fitsSystemWindows="true"                android:scaleType="centerCrop"                android:src="@mipmap/bg"                app:layout_collapseMode="parallax" />            <android.support.v7.widget.Toolbar                android:id="@+id/toolbar"                android:layout_width="match_parent"                android:layout_height="?attr/actionBarSize"                app:layout_collapseMode="pin"                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />        </android.support.design.widget.CollapsingToolbarLayout>    </android.support.design.widget.AppBarLayout>    <!--第二部分:主要内容,NestedScrollView和ScrollView基本功能一致,只不过NestedScrollView可以兼容新的控件-->    <android.support.v4.widget.NestedScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        app:layout_behavior="@string/appbar_scrolling_view_behavior">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical"            android:paddingTop="24dp">            <!--卡片布局-->            <android.support.v7.widget.CardView                android:layout_width="match_parent"                android:layout_height="200dp"                android:layout_margin="10dp">                <LinearLayout                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:orientation="vertical">                    <TextView                        android:layout_width="match_parent"                        android:layout_height="wrap_content"                        android:text="Info"                        android:textAppearance="@style/TextAppearance.AppCompat.Title" />                    <TextView                        android:layout_width="match_parent"                        android:layout_height="wrap_content"                        android:text="here testing" />                </LinearLayout>            </android.support.v7.widget.CardView>            <android.support.v7.widget.CardView                android:layout_width="match_parent"                android:layout_height="200dp"                android:layout_marginBottom="10dp"                android:layout_marginLeft="10dp"                android:layout_marginRight="10dp">                <LinearLayout                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:orientation="vertical">                    <TextView                        android:layout_width="match_parent"                        android:layout_height="wrap_content"                        android:text="Friends"                        android:textAppearance="@style/TextAppearance.AppCompat.Title" />                    <TextView                        android:layout_width="match_parent"                        android:layout_height="wrap_content"                        android:text="hange testing" />                </LinearLayout>            </android.support.v7.widget.CardView>            <android.support.v7.widget.CardView                android:layout_width="match_parent"                android:layout_height="200dp"                android:layout_marginBottom="10dp"                android:layout_marginLeft="10dp"                android:layout_marginRight="10dp">                <LinearLayout                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:orientation="vertical">                    <TextView                        android:layout_width="match_parent"                        android:layout_height="wrap_content"                        android:text="Related"                        android:textAppearance="@style/TextAppearance.AppCompat.Title" />                    <TextView                        android:layout_width="match_parent"                        android:layout_height="wrap_content"                        android:text="wwww dddd" />                </LinearLayout>            </android.support.v7.widget.CardView>        </LinearLayout>    </android.support.v4.widget.NestedScrollView>    <!--第三部分:漂浮按钮-->    <android.support.design.widget.FloatingActionButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="20dp"        android:clickable="true"        android:src="@mipmap/ic_launcher"        app:layout_anchor="@id/appbar"        app:layout_anchorGravity="bottom|right|end" /></android.support.design.widget.CoordinatorLayout>

        代码如下:

//给页面设置工具栏final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);setSupportActionBar(toolbar);getSupportActionBar().setDisplayHomeAsUpEnabled(true);//设置工具栏标题CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);collapsingToolbar.setTitle("cheeseName");


        总的来说,NestedScrollView是一个简单的控件,作用跟ScrollView基本没差别,关键在于跟ToolBar交互产生的作用,所以应该注意的是,

app:layout_behavior="@string/appbar_scrolling_view_behavior"
这一句,这句话跟CollapsingToolbarLayout中

app:layout_scrollFlags="scroll|exitUntilCollapsed"
对应,系统一旦发现NestedScrollView中有那句话,就会检查其他控件,然后如果有上面这句对应的话,那么滑动效果就是可以实现的。就这一点来说,他的使用基本上和RecyclerView一样。
原创粉丝点击