详解Android使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果

 更新时间:2020年05月22日 09:56:20   作者:文休  
这篇文章主要为大家详细介绍了Android使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果

如何使用 CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现下面GIF图中的效果,再展开的时候头像处于红白中间,根据收缩程度改变头像的位置!底下的RecyclerView也跟随这个移动,不会出现中间隔出一段距离!(仅提供源码复制粘贴,很简单的)

先看下效果图:

下面上代码

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"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <android.support.design.widget.CoordinatorLayout
    android:id="@+id/Fragment_ontstf_CoordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false">

    <android.support.design.widget.AppBarLayout
      android:id="@+id/Fragment_ontstf_AppBar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/cFF3523"
      android:clipChildren="false"
      app:elevation="0dp">

      <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/Fragment_ontstf_CollapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <RelativeLayout
          android:id="@+id/Fragment_ontstf_RelativeLayout"
          android:layout_width="match_parent"
          android:layout_height="200dp"
          android:orientation="horizontal"
          app:layout_collapseMode="parallax"
          app:layout_collapseParallaxMultiplier="0.25">

          <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            app:cardCornerRadius="5dp"
            app:cardElevation="5dp">

            <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal">

              <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:gravity="center_horizontal|bottom"
                  android:text="331"
                  android:textColor="@color/c333333"
                  android:textSize="16sp" />

                <TextView
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:gravity="center_horizontal"
                  android:text="积分"
                  android:textColor="@color/c333333"
                  android:textSize="10sp" />

              </LinearLayout>

              <View
                android:layout_width="0.7px"
                android:layout_height="40dp"
                android:layout_gravity="center_vertical"
                android:background="#727272"></View>

              <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:gravity="center_horizontal|bottom"
                  android:text="5"
                  android:textColor="@color/c333333"
                  android:textSize="16sp" />

                <TextView
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:gravity="center_horizontal"
                  android:text="优惠卷"
                  android:textColor="@color/c333333"
                  android:textSize="10sp" />

              </LinearLayout>

              <View
                android:layout_width="0.7px"
                android:layout_height="40dp"
                android:layout_gravity="center_vertical"
                android:background="#727272"></View>

              <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:gravity="center_horizontal|bottom"
                  android:text="0.00"
                  android:textColor="@color/c333333"
                  android:textSize="16sp" />

                <TextView
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:gravity="center_horizontal"
                  android:text="代金卷"
                  android:textColor="@color/c333333"
                  android:textSize="10sp" />

              </LinearLayout>

              <View
                android:layout_width="0.7px"
                android:layout_height="40dp"
                android:layout_gravity="center_vertical"
                android:background="#727272"></View>

              <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:gravity="center_horizontal|bottom"
                  android:text="3314"
                  android:textColor="@color/c333333"
                  android:textSize="16sp" />

                <TextView
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:gravity="center_horizontal"
                  android:text="淘币"
                  android:textColor="@color/c333333"
                  android:textSize="10sp" />

              </LinearLayout>

            </LinearLayout>

          </android.support.v7.widget.CardView>

        </RelativeLayout>

        <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="50dp"
          android:background="@color/cFF3523"
          android:gravity="center_vertical"
          app:layout_collapseMode="pin">

          <ImageView
            android:id="@+id/Fragment_ontstf_set"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@id/Fragment_ontstf_message"
            android:src="@mipmap/set" />

          <ImageView
            android:id="@+id/Fragment_ontstf_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:src="@mipmap/message" />

        </RelativeLayout>

        <android.support.v7.widget.Toolbar
          android:id="@+id/Fragment_ontstf_Toolbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          app:contentInsetStart="0dp"
          app:layout_collapseMode="pin">

          <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:padding="5dp">

            <de.hdodenhof.circleimageview.CircleImageView
              android:id="@+id/Fragment_ontstf_portrait"
              android:layout_width="45dp"
              android:layout_height="45dp"
              android:layout_gravity="center_vertical"
              android:src="@mipmap/ic_launcher_round" />

            <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

              <TextView
                android:id="@+id/Fragment_ontstf_name"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_marginLeft="10dp"
                android:textSize="18sp" />

              <TextView
                android:id="@+id/Fragment_ontstf_introduce"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_marginLeft="10dp"
                android:layout_marginBottom="5dp"
                android:textSize="14sp" />

            </LinearLayout>

          </LinearLayout>

        </android.support.v7.widget.Toolbar>

      </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
      android:id="@+id/Fragment_ontstf_RecyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/c00000000"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v7.widget.RecyclerView>

  </android.support.design.widget.CoordinatorLayout>

</LinearLayout>

JAVA代码如下:

 /**
   * 获取控件信息
   */
 private void initView() {
    FragmentOntstfAppBar = (AppBarLayout) view.findViewById(R.id.Fragment_ontstf_AppBar);
    FragmentOntstfToolbar = (Toolbar) view.findViewById(R.id.Fragment_ontstf_Toolbar);
    FragmentOntstfPortrait = (CircleImageView) view.findViewById(R.id.Fragment_ontstf_portrait);
    FragmentOntstfName = (TextView) view.findViewById(R.id.Fragment_ontstf_name);
    FragmentOntstfIntroduce = (TextView) view.findViewById(R.id.Fragment_ontstf_introduce);
    FragmentOntstfCollapsingToolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.Fragment_ontstf_CollapsingToolbarLayout);
    FragmentOntstfSet = (ImageView) view.findViewById(R.id.Fragment_ontstf_set);
    FragmentOntstfMessage = (ImageView) view.findViewById(R.id.Fragment_ontstf_message);
    FragmentOntstfRecyclerView = (RecyclerView) view.findViewById(R.id.Fragment_ontstf_RecyclerView);
    FragmentOntstfCoordinatorLayout = (CoordinatorLayout) view.findViewById(R.id.Fragment_ontstf_CoordinatorLayout);
    FragmentOntstfRelativeLayout = (RelativeLayout) view.findViewById(R.id.Fragment_ontstf_RelativeLayout);
    setFragmentOntstfRecyclerView();
    AppBar();
    mPresenter.PselectUser(Userid);
  }

  /**
   * 最主要的代码
   * AppBar滑动效果
   */
  private void AppBar() {
    FragmentOntstfAppBar.setExpanded(true);
    FragmentOntstfAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
      @Override
      public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
        float alpha = (float) Math.abs(i) / appBarLayout.getTotalScrollRange();
        if (alpha == 0) {
          CollapsingToolbarLayout.LayoutParams layoutParams = new CollapsingToolbarLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
          FragmentOntstfRecyclerView.setPadding(0,FragmentOntstfToolbar.getHeight() / 2,0,0);
          layoutParams.setMargins(0, 0, 0, (-FragmentOntstfToolbar.getHeight() / 2));
          layoutParams.gravity = Gravity.BOTTOM;
          FragmentOntstfToolbar.setLayoutParams(layoutParams);
          FragmentOntstfCoordinatorLayout.setClipChildren(false);
        } else if (FragmentOntstfRelativeLayout.getHeight() - Math.abs(i * 1.0f) == FragmentOntstfToolbar.getHeight()) {
          FragmentOntstfCoordinatorLayout.setClipChildren(true);
        } else {
          int a = (int) ((FragmentOntstfToolbar.getHeight() / 2) * alpha);
          CollapsingToolbarLayout.LayoutParams layoutParams = new CollapsingToolbarLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
          FragmentOntstfRecyclerView.setPadding(0,FragmentOntstfToolbar.getHeight() / 2 - a,0,0);
          layoutParams.setMargins(0, 0, 0, -(FragmentOntstfToolbar.getHeight() / 2) - (-a));
          layoutParams.gravity = Gravity.BOTTOM;
          FragmentOntstfToolbar.setLayoutParams(layoutParams);
          FragmentOntstfCoordinatorLayout.setClipChildren(false);
        }
      }
    });
  }

这样就完成了!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android之高效加载大图的方法示例

    Android之高效加载大图的方法示例

    这篇文章主要介绍了Android之高效加载大图的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • Android二级缓存加载图片实现照片墙功能

    Android二级缓存加载图片实现照片墙功能

    这篇文章主要为大家详细介绍了Android二级缓存加载图片实现照片墙功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Android实现纸飞机的简单操作

    Android实现纸飞机的简单操作

    这篇文章主要为大家详细介绍了Android实现纸飞机的简单操作,类似于漂流瓶功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android开发ThreadPoolExecutor与自定义线程池详解

    Android开发ThreadPoolExecutor与自定义线程池详解

    这篇文章主要为大家介绍了Android开发ThreadPoolExecutor与自定义线程池详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Android5.x中的阴影效果elevation和translationZ的实现方法

    Android5.x中的阴影效果elevation和translationZ的实现方法

    这篇文章主要介绍了 android5.x中的阴影效果elevation和translationZ的相关资料,需要的朋友可以参考下
    2016-12-12
  • Android 开发与代码无关技巧详解

    Android 开发与代码无关技巧详解

    这篇文章主要为大家介绍了Android 开发与代码无关技巧详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Kotlin中的Checked Exception机制浅析

    Kotlin中的Checked Exception机制浅析

    这篇文章主要给大家介绍了关于Kotlin中Checked Exception机制的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • OpenGL中的glutInitDisplayMode()函数的理解

    OpenGL中的glutInitDisplayMode()函数的理解

    今天小编就为大家分享一篇关于OpenGL中的glutInitDisplayMode()函数的理解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-04-04
  • Java程序员转Android开发必读经验一份

    Java程序员转Android开发必读经验一份

    小编最近几日偷偷的发现部分Java程序员想转安卓开发,故此加紧补充知识,为大家搜集资料,积极整理前人的经验,希望可以给正处于困惑中的你,带来些许的帮助。
    2017-11-11
  • android 使用浏览器打开指定页面的实现方法

    android 使用浏览器打开指定页面的实现方法

    这篇文章主要介绍了android 使用浏览器打开指定页面的实现方法,本文通过实例文字说明的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-06-06

最新评论