Android-CoordinatorLayout.……Androidandroid技术

Android SmartRefreshLayout + Coo

2021-06-08  本文已影响0人  kennychaos

融合 CollapsingToolbarLayout +SmartRefreshLayout 的效果

也提供了github的地址 : https://github.com/KennyChaos7/SmartRefreshLayout-CoordinatorLayout-AppBarLayout.git

device-2021-06-08-104734.png device-2021-06-08-104753.png device-2021-06-08-104824.png device-2021-06-08-104941.png

当然,按照国际惯例,先上个效果图

     <LinearLayout
      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:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      android:orientation="vertical">
        <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/ic_launcher_background">
          <TextView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:text="header"
          android:gravity="center"/>
        </FrameLayout>
        <com.scwang.smart.refresh.layout.SmartRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:srlEnableRefresh="true"
        app:srlEnableLoadMore="true"
        android:id="@+id/smartrefreshlayout"
        tools:context=".ScrollingActivity">
      <com.scwang.smart.refresh.header.ClassicsHeader
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
      <androidx.coordinatorlayout.widget.CoordinatorLayout
      android:id="@+id/coordinatorlayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true">
      <com.google.android.material.appbar.AppBarLayout
      android:id="@+id/app_bar"
      android:layout_width="match_parent"
      android:layout_height="250dp"
      android:fitsSystemWindows="true"
      android:theme="@style/Theme.MyApplication.AppBarOverlay">
     
      <com.google.android.material.appbar.CollapsingToolbarLayout
      android:id="@+id/toolbar_layout"
      android:layout_width="match_parent"
      android:layout_height="200dp"
      android:fitsSystemWindows="true"
      app:contentScrim="?attr/colorPrimary"
      app:layout_scrollFlags="scroll|exitUntilCollapsed"
      app:toolbarId="@+id/toolbar">
      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      app:layout_collapseMode="parallax">
      <TextView
      android:layout_width="match_parent"
      android:layout_height="150dp"
      android:text="CENTER"
      android:gravity="center"
      android:textSize="22sp"
      android:textColor="@color/black"/>
      </LinearLayout>
      <androidx.appcompat.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:layout_collapseMode="pin"
      app:popupTheme="@style/Theme.MyApplication.PopupOverlay"
      android:layout_gravity="bottom">
      <TextView
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:textColor="@color/white"
      android:layout_gravity="bottom"
      android:gravity="center"
      android:text="1"
      android:textSize="18sp"/>
      </androidx.appcompat.widget.Toolbar>
      </com.google.android.material.appbar.CollapsingToolbarLayout>
      <TextView
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:textColor="@color/white"
      android:layout_gravity="bottom"
      android:gravity="center"
      android:text="2"
      android:textSize="18sp"/>
      </com.google.android.material.appbar.AppBarLayout>
      <include layout="@layout/content_scrolling" />
      </androidx.coordinatorlayout.widget.CoordinatorLayout>
      <com.scwang.smart.refresh.footer.ClassicsFooter
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
      </com.scwang.smart.refresh.layout.SmartRefreshLayout>
     </LinearLayout>
>   这里就不贴Activity了,因为并没有所有的效果实现并不需要Activity的参与

如果做到这一步,你就会发现,其实这里的效果跟实际的效果图的效果不一致,或者应该说完全没有想要的效果。
这是因为下面这两行代码

      <androidx.appcompat.widget.Toolbar
      android:layout_height="wrap_content"
      ...
      android:layout_gravity="bottom">
      ...
      </androidx.appcompat.widget.Toolbar>
     ... 

这里我们把Toolbar设置成了位于CollapsingToolbarLayout的最底部,而且由于高度没有限制,是随着子布局的扩大而自适应的wrap_content,从而使得在滑动时AppBarLayout计算CollapsingToolbarLayout与顶边的高度为0,没有可以折叠的距离,从而使得整个折叠效果不存在

解决方法也很简单,三选一即可:

  1. 去掉android:layout_gravity="bottom",改用android:layout_marginTop="xx"来间接设置可折叠的距离
  2. 给定Toolbar一个固定的高度
  3. Toolbar设置1px的高度,把折叠后要显示的布局放在CollapsingToolbarLayoutToolbar同级 (如果你不希望你的布局会在下拉加载时被折叠,则最好使用此方法)

最后给个正常有效果的xml看看吧

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/ic_launcher_background">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="header"
            android:gravity="center"/>
    </FrameLayout>
    <com.scwang.smart.refresh.layout.SmartRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:srlEnableRefresh="true"
        app:srlEnableLoadMore="true"
        android:id="@+id/smartrefreshlayout"
        tools:context=".ScrollingActivity">
        <com.scwang.smart.refresh.header.ClassicsHeader
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:id="@+id/coordinatorlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">
            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/app_bar"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:fitsSystemWindows="true"
                android:theme="@style/Theme.MyApplication.AppBarOverlay">

                <com.google.android.material.appbar.CollapsingToolbarLayout
                    android:id="@+id/toolbar_layout"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:fitsSystemWindows="true"
                    app:contentScrim="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed"
                    app:toolbarId="@+id/toolbar">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        app:layout_collapseMode="parallax">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="150dp"
                            android:text="CENTER"
                            android:gravity="center"
                            android:textSize="22sp"
                            android:textColor="@color/black"/>
                    </LinearLayout>
                    <androidx.appcompat.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        app:layout_collapseMode="pin"
                        android:layout_gravity="bottom"
                        app:popupTheme="@style/Theme.MyApplication.PopupOverlay">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            android:textColor="@color/white"
                            android:layout_gravity="center"
                            android:gravity="center"
                            android:text="1"
                            android:textSize="18sp"/>
                    </androidx.appcompat.widget.Toolbar>
                </com.google.android.material.appbar.CollapsingToolbarLayout>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:textColor="@color/white"
                    android:layout_gravity="bottom"
                    android:gravity="center"
                    android:text="2"
                    android:textSize="18sp"/>
            </com.google.android.material.appbar.AppBarLayout>
            <include layout="@layout/content_scrolling" />
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
        <com.scwang.smart.refresh.footer.ClassicsFooter
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </com.scwang.smart.refresh.layout.SmartRefreshLayout>
</LinearLayout>
上一篇下一篇

猜你喜欢

热点阅读