使用CoordinatorLayout打造各种炫酷的效果

结合ViewPager

ViewPager

结合ViewPager的视觉特差


AppBarLayout


它是继承与LinearLayout的,默认 的 方向 是Vertical

| 类型 | 说明 |

| — | — |

| int SCROLL_FLAG_ENTER_ALWAYS | When entering (scrolling on screen) the view will scroll on any downwards scroll event, regardless of whether the scrolling view is also scrolling. |

| int SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED | An additional flag for ‘enterAlways’ which modifies the returning view to only initially scroll back to it’s collapsed height. |

| int SCROLL_FLAG_EXIT_UNTIL_COLLAPSED | When exiting (scrolling off screen) the view will be scrolled until it is ‘collapsed’. |

| int SCROLL_FLAG_SCROLL | The view will be scroll in direct relation to scroll events. |

| int SCROLL_FLAG_SNAP | Upon a scroll ending, if the view is only partially visible then it will be snapped and scrolled to it’s closest edge. |

| 类型 | 说明 |

| — | — |

| int SCROLL_FLAG_ENTER_ALWAYS | W((entering) / (scrolling on screen))下拉的时候,这个View也会跟着滑出。 |

| int SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED | 另一种enterAlways,但是只显示折叠后的高度。 |

| int SCROLL_FLAG_EXIT_UNTIL_COLLAPSED | ((exiting) / (scrolling off screen))上拉的时候,这个View会跟着滑动直到折叠。 |

| int SCROLL_FLAG_SCROLL | 这个View将会响应Scroll事件 |

| int SCROLL_FLAG_SNAP | 在Scroll滑动事件结束以前 ,如果这个View部分可见,那么这个View会停在最接近当前View的位置 |

我们可以通过两种 方法设置这个Flag

  • 方法一

setScrollFlags(int)

  • 方法二

app:layout_scrollFlags=“scroll|enterAlways”

注意事项

AppBarLayout必须作为CoordinatorLayout的直接子View,否则它的大部分功能将不会生效,如layout_scrollFlags等。

首先我们先来看一下我们 效果图一是怎样实现的


代码

<android.support.design.widget.CoordinatorLayout

android:id=“@+id/main_content”

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.support.design.widget.AppBarLayout

android:id=“@+id/appbar”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:theme=“@style/ThemeOverlay.AppCompat.Dark.ActionBar”>

<android.support.v7.widget.Toolbar

android:id=“@+id/toolbar”

android:layout_width=“match_parent”

android:layout_height=“?attr/actionBarSize”

android:background=“?attr/colorPrimary”

app:layout_scrollFlags=“scroll|enterAlways”

app:popupTheme=“@style/ThemeOverlay.AppCompat.Light”/>

.

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

<android.support.v7.widget.RecyclerView

android:id=“@+id/recyclerView”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

app:layout_behavior=“@string/appbar_scrolling_view_behavior”/>

<android.support.design.widget.FloatingActionButton

android:id=“@+id/fab”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“end|bottom”

android:layout_margin=“15dp”

android:src=“@drawable/add_2”/>

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

思路 分析

从图中我们可以知道 layout_scrollFlags="scroll|enterAlways,

前面已经说到layout_scrollFlags=scroll的时候,这个View会 跟着 滚动 事件响应,

layout_scrollFlags=“enterAlways”的时候 这个View会响应下拉事件

所以呈现出来的结果应该是我们在上拉的时候toolBar 会隐藏,下拉的时候toolBar会出来

那如果当我们的toolBar 等于 app:layout_scrollFlags="scroll|snap"的时候 ,

layout_scrollFlags=scroll的时候,这个View会 跟着 滚动 事件响应,

layout_scrollFlags=“snap”的时候 在Scroll滑动事件结束以前 ,如果这个View部分可见,那么这个View会停在最接近当前View的位置。

综上呈现的效果如下,代码见ToolBarSampleSnar的布局文件

结合ViewPager


布局代码如下

<android.support.design.widget.CoordinatorLayout

android:id=“@+id/main_content”

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.support.design.widget.AppBarLayout

android:layout_width=“match_parent”

android:layout_height=“250dp”>

<ImageView android:layout_width=“match_parent”

android:layout_height=“200dp”

android:background=“?attr/colorPrimary”

android:scaleType=“fitXY”

android:src=“@drawable/tangyan”

app:layout_scrollFlags=“scroll|enterAlways”/>

<android.support.design.widget.TabLayout

android:id=“@+id/tabs”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_alignParentBottom=“true”

android:background=“?attr/colorPrimary”

app:tabIndicatorColor=“@color/colorAccent”

app:tabIndicatorHeight=“4dp”

app:tabSelectedTextColor=“#000”

app:tabTextColor=“#fff”/>

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

<android.support.v4.view.ViewPager

android:id=“@+id/viewpager”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

app:layout_behavior=“@string/appbar_scrolling_view_behavior”/>

<android.support.design.widget.FloatingActionButton

android:id=“@+id/fab”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“end|bottom”

android:layout_margin=“15dp”

android:src=“@drawable/add_2”/>

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

思路分析

其实相对于前 一个例子,只是把 摆放RecyclerView 的位置替换成ViewPager而已,为了有页面导航器的效果,再使用 TabLayout而已,而TabLayout 在我们滑动的时候最终会停靠在 最顶部,是因为我们没有设置其layout_scrollFlags,即TabLayout是静态的

运行以后,即可看到以下的结果

ViewPager

下面我们一起来看一下 TabLayout是怎样结合ViewPager直线 导航器的效果的

代码注释 里面已经解释地很清楚了 ,这里我就不解释了

public class ViewPagerSample extends AppCompatActivity {

ViewPager mViewPager;

List mFragments;

String[] mTitles = new String[]{

“主页”, “微博”, “相册”

};

private TabLayout mTabLayout;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_third);

// 第一步,初始化ViewPager和TabLayout

mViewPager = (ViewPager) findViewById(R.id.viewpager);

mTabLayout = (TabLayout) findViewById(R.id.tabs);

setupViewPager();

}

private void setupViewPager() {

mFragments = new ArrayList<>();

for (int i = 0; i < mTitles.length; i++) {

ListFragment listFragment = ListFragment.newInstance(mTitles[i]);

mFragments.add(listFragment);

}

// 第二步:为ViewPager设置适配器

BaseFragmentAdapter adapter =

new BaseFragmentAdapter(getSupportFragmentManager(), mFragments, mTitles);

mViewPager.setAdapter(adapter);

// 第三步:将ViewPager与TableLayout 绑定在一起

mTabLayout.setupWithViewPager(mViewPager);

}

}

如果我们想更改Indicator的相关样式,我们可以在布局文件里面使用

<android.support.design.widget.TabLayout

android:id=“@+id/tabs”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_alignParentBottom=“true”

android:background=“?attr/colorPrimary”

app:tabIndicatorColor=“@color/colorAccent”

app:tabIndicatorHeight=“4dp”

app:tabSelectedTextColor=“#000”

app:tabTextColor=“#fff”/>

如果你不想使用Google 帮我们 封装好的控件的话,你也可以自己自定义一个控件,你可以参考我的这一篇博客仿网易新闻的顶部导航指示器


在看例子结合ViewPager的视觉特差之前 ,我们需要先了解CollapsingToolbarLayout这个控件

CollapsingToolbarLayout


CollapsingToolbarLayout继承与FrameLayout,官网地址,请自备梯子。

简单来说 ,CollapsingToolbarLayout是工具栏的包装器,它通常作为AppBarLayout的孩子。主要实现以下功能

  • Collapsing title(可以折叠 的 标题 )

  • Content scrim(内容装饰),当我们滑动的位置 到达一定阀值的时候,内容 装饰将会被显示或者隐藏

  • Status bar scrim(状态栏布)

  • Parallax scrolling children,滑动的时候孩子呈现视觉特差效果

  • Pinned position children,固定位置的 孩子

下面我们一起来看一下几个常量

| 常量 | 解释说明 |

| — | — |

| int COLLAPSE_MODE_OFF | The view will act as normal with no collapsing behavior.(这个 View将会 呈现正常的结果,不会表现出折叠效果) |

| int COLLAPSE_MODE_PARALLAX | The view will scroll in a parallax fashion. See setParallaxMultiplier(float) to change the multiplier used.(在滑动的时候这个View 会呈现 出 视觉特差效果 ) |

| int COLLAPSE_MODE_PIN | The view will pin in place until it reaches the bottom of the CollapsingToolbarLayout.(当这个View到达 CollapsingToolbarLayout的底部的时候,这个View 将会被放置,即代替整个CollapsingToolbarLayout) |

我们有两种方法可以设置这个常量,

方法一:在代码中使用这个方法

setCollapseMode(int collapseMode)

方法 二:在布局文件中使用自定义属性

app:layout_collapseMode=“pin”

到此 ,CollapsingToolbarLayout的一些重要属性已经讲解完毕,下面我们一起来看一下我们是怎样结合ViewPager实现视差效果的


结合ViewPager的视觉特差


布局代码

<?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:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background=“@android:color/background_light”

android:fitsSystemWindows=“true”

<android.support.design.widget.AppBarLayout

android:id=“@+id/main.appbar”

android:layout_width=“match_parent”

android:layout_height=“300dp”

android:fitsSystemWindows=“true”

android:theme=“@style/ThemeOverlay.AppCompat.Dark.ActionBar”

<android.support.design.widget.CollapsingToolbarLayout

android:id=“@+id/main.collapsing”

android:layout_width=“match_parent”

android:layout_height=“250dp”

android:fitsSystemWindows=“true”

app:contentScrim=“?attr/colorPrimary”

app:expandedTitleMarginEnd=“64dp”

app:expandedTitleMarginStart=“48dp”

app:layout_scrollFlags=“scroll|exitUntilCollapsed”

<ImageView

android:id=“@+id/main.backdrop”

android:layout_width=“match_parent”

文末

架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。

移动架构师

系统学习技术大纲

一线互联网Android面试题总结含详解(初级到高级专题)

image

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
ntentScrim=“?attr/colorPrimary”

app:expandedTitleMarginEnd=“64dp”

app:expandedTitleMarginStart=“48dp”

app:layout_scrollFlags=“scroll|exitUntilCollapsed”

<ImageView

android:id=“@+id/main.backdrop”

android:layout_width=“match_parent”

文末

架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。

[外链图片转存中…(img-YqecHDiT-1715185897809)]

[外链图片转存中…(img-3GY2O3FL-1715185897810)]

一线互联网Android面试题总结含详解(初级到高级专题)

[外链图片转存中…(img-a31cT6rD-1715185897810)]

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 27
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值