Android 中CoordinatorLayout与AppBarLayout、NestedScrollView用法(实现上滑动悬浮,全局可滑动)

1.关于coordinatorLayout与AppBarLayout的文章太多【coordinatorLayout是design包中控件

简单说就是:当向上滑动内容时AppBarLayout中的内容会隐藏掉,app:layout_behavior="@string/appbar_scrolling_view_behavior"修饰的LinearLayout中的内容会悬浮在顶部,

最下面需要使用recyclerView来加载数据

方法:

appbarlayout.setExpanded(true);//表示展开【会下滑动展开上滑动隐藏的内容】

先看布局代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <include
                layout="@layout/layout_activedetail_head"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <CheckBox
                android:id="@+id/desc_c"
                style="@style/actived_checkbox"
                android:layout_marginTop="0dp"
                android:text="@string/active_detail" />
        </android.support.v4.widget.NestedScrollView>

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone">

            <TextView
                android:id="@+id/desc_t"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/line_top"
                android:padding="10dp"
                android:textColor="@color/balck_title"
                android:textSize="12sp"
                android:visibility="gone" />
        </android.support.v4.widget.NestedScrollView>

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            >

            <CheckBox
                android:id="@+id/activesp_t"
                style="@style/actived_checkbox"
                android:layout_marginTop="0dp"
                android:text="@string/active_sp" />
        </android.support.v4.widget.NestedScrollView>

        <LinearLayout
            android:id="@+id/activesp_l"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/activesp_rv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            >

            <CheckBox
                android:id="@+id/activefile_c"
                style="@style/actived_checkbox"
                android:layout_marginTop="0dp"
                android:text="@string/active_file" />
        </android.support.v4.widget.NestedScrollView>


        <LinearLayout
            android:id="@+id/activefile_l"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/line_top"
            android:visibility="gone">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/activefile_rv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>
2.LinearLayout中的checkbox本来是没法滑动的,外层添加了NestedScrollView,它就可以控制着整个布局一起上下滑动。具体效果需自己去实践哈。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值