详解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);
        }
      }
    });
  }

这样就完成了!

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

(0)

相关推荐

  • Android中View跟随手指滑动效果的实例代码

    本文讲述了Android中View跟随手指滑动效果的实例代码.分享给大家供大家参考,具体如下: 1.android View 主要6种滑动方法,分别是 layout() offsetLeftAndRight()和offsetTopAndBottom() LayoutParams scrollBy()和 scrollTo() Scroller 动画 2.实现效果图 3.自定义中使用layout()方法实习view的滑动 public class MoveView extends View { pr

  • Android视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现

    本文给大家分享Android视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现,具体代码如下所示: import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import andro

  • 封装的android监听手指左右滑动屏幕的事件类分享

    左右滑动是智能手机最常用的动作,在此简单的封装了一下,以后直接拿来用就可以了. 简单的只需要几行就可以了,下面那个类是封装好了的. package com.example.test; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.util.Log; import android.widget.RelativeLayout; public c

  • Android中解决页签手指按下从左到右滑动的bug

    有一种方法可以阻止父层的View截获touch事件,就是调用 getParent().requestDisallowInterceptTouchEvent(true);方法. 一旦底层View收到touch的 action后调用这个方法那么父层View就不会再调用onInterceptTouchEvent了,也无法截获以后的action 在ViewPagerIndicator项目中找到TabPageIndicator该类,添加如下代码 @Override public boolean dispa

  • Android通过ImageView设置手指滑动控件缩放

    ImageView设置手指滑动缩放效果,具体实现步骤大家通过本文学习下吧! 实现步骤 1, imageview设置scaletype为 android:scaleType="matrix" 2, 设置imageview的setOnTouchListener,重写里面的代码 3, 新建一个matrix, matrix.postScale(scale,scale,缩放中心,缩放中心); image.setImageMatrix(matrix); 这样image的大小就会改变了. 需要注意的

  • android中使用Activity实现监听手指上下左右滑动

    用Activity的onTouchEvent方法实现监听手指上下左右滑动 应用了Activity的ontouchEvent方法监听手指点击事件,手指滑动的时候会先按下,滑倒另一个地方再抬起,我们就可以根据按下的坐标和抬起的坐标算出用户是往哪一个方向滑动了. package com.example.testtt; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; impor

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

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

  • 详解Android使用CoordinatorLayout+AppBarLayout实现拉伸顶部图片功能

    一.国际惯例,先看下效果图 二.不跟你多bb直接上布局文件代码 <?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:tool

  • 详解Android如何实现好的弹层体验效果

    目录 前言 弹层的形式选择 中间弹层 左右抽屉弹层 顶部弹层 底部弹层 总结 前言 当前 App 的设计趋势越来越希望给用户沉浸式体验,这种设计会让用户尽量停留在当前的界面,而不需要太多的跳转,这就需要引入弹层.比如,抖音引入购物功能后,就实现了在观看视频界面可以通过弹层完成加入购物车.下单操作,无需离开当前的视频界面.本篇我们就来讲讲弹层这块需要注意哪些用户体验. 弹层的形式选择 弹层从形式上来说有中间弹层.左侧弹层.右侧弹层.底部弹层和顶部弹层,如下图所示. 移动端经过这么多年的发展,不同的

  • 详解Android中Intent对象与Intent Filter过滤匹配过程

    如果对Intent不是特别了解,可以参见博文<详解Android中Intent的使用方法>,该文对本文要使用的action.category以及data都进行了详细介绍.如果想了解在开发中常见Intent的使用,可以参见<Android中Intent习惯用法>. 本文内容有点长,希望大家可以耐心读完. 本文在描述组件在manifest中注册的Intent Filter过滤器时,统一用intent-filter表示. 一.概述 我们知道,Intent是分两种的:显式Intent和隐式

  • 详解Android Webview加载网页时发送HTTP头信息

    详解Android Webview加载网页时发送HTTP头信息 当你点击一个超链接进行跳转时,WebView会自动将当前地址作为Referer(引荐)发给服务器,因此很多服务器端程序通过是否包含referer来控制盗链,所以有些时候,直接输入一个网络地址,可能有问题,那么怎么解决盗链控制问题呢,其实在webview加载时加入一个referer就可以了,如何添加呢? 从Android 2.2 (也就是API 8)开始,WebView新增加了一个接口方法,就是为了便于我们加载网页时又想发送其他的HT

  • 详解Android获得系统GPU参数 gl.glGetString

    详解Android获得系统GPU参数 gl.glGetString 通过文档的查找,以及源码的剖析,Android的GPU信息需要通过OpenGL来获取,android framework层提供GL10来获取相应的参数,而GL10要在使用自定义的View时才可以获得,下面是获得GPU信息的例子: 1.实现Render类 class DemoRenderer implements GLSurfaceView.Renderer { public void onSurfaceCreated(GL10

  • 详解Android通过修改配置文件设置wifi密码

    详解Android通过修改配置文件设置wifi密码 前言: 在一些非常规Android设备上,如眼镜/手表,输入wifi密码如同一场灾难.此时可以通过修改配置文件的方法设置wifi的ssid和密码. wifi密码配置文件 首先要保证设备已经root,wifi的配置文件在/data/misc/wifi/wpa_supplicant.conf,可以先将其pull出来,然后在下面加上network开头的那部分就ok了.然后再导入进去.下面是我的配置文件: ##### wpa_supplicant co

  • 详解Android更改APP语言模式的实现过程

    一.效果图 二.描述 更改Android项目中的语言,这个作用于只用于此APP,不会作用于整个系统 三.解决方案 (一)布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" a

  • 详解android 通过uri获取bitmap图片并压缩

    详解android 通过uri获取bitmap图片并压缩 很多人在调用图库选择图片时会在onActivityResult中用Media.getBitmap来获取返回的图片,如下: Uri mImageCaptureUri = data.getData(); Bitmap photoBmp = null; if (mImageCaptureUri != null) { photoBmp = MediaStore.Images.Media.getBitmap(ac.getContentResolve

  • 详解Android TableLayout中stretchColumns、shrinkColumns的用法

    详解Android 中TableLayout中stretchColumns.shrinkColumns的用法 android:stretchColumns="1" android:shrinkColumns="1"这两个属性是TableLayout所特有的,也是这两个属性影响了子对象的布局. 表格布局是按照行列来组织子视图的布局.表格布局包含一系列的Tabrow对象,用于定义行(也可以使用其它子对象).表格布局不为它的行.列和单元格显示表格线.每个行可以包含个以上(

随机推荐