Hi,你们好,我是笔墨Android!相信不少小伙伴在实际开发中都有这样的需求,一个列表滚动到某一位置,而后有一个按钮,回到顶部?很常见的一个效果,在之前咱们通常都使用一个图片,放到那里。可是在5.0的时候google推出了FloatingActionButton,并作了相应的兼容,能很好的解决以上问题,而且经过CoordinatorLayout能够很好的联动!android
FloatingActionButton是一个继承ImageView悬浮的动做按钮,常常用在一些比较经常使用的操做中,一个页面尽可能只有一个FloatingActionButton,不然会给用户一种错乱的感受!FloatingActionButton的大小分为标准型(56dp)和迷你型(40dp),google是这么要求的,若是你不喜欢能够本身设置其余大小。而且对于图标进行使用materialDesign的图标,大小在24dp为最佳!git
先来一张效果图程序员
其实FloatingActionButton的用法很简单,主要是在布局文件中定义就能够了,这里先将一下各个属性的含义: 你们能够试一下,能更好的理解相应的内容的!github
整理的布局是这样的:bash
<?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">
<LinearLayout
android:id="@+id/ll_top"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#438b8b"
android:orientation="horizontal">
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="伪装这有内容内容" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:src="@mipmap/ic_call_white_24dp"
app:backgroundTint="#FF3030"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal"
app:layout_anchor="@id/ll_top"
app:layout_anchorGravity="bottom|right"
app:pressedTranslationZ="120dp"
app:rippleColor="#00ff00" />
</android.support.design.widget.CoordinatorLayout>
复制代码
就是上面这样了,忘说了一件很重要的事情......FloatingActionButton的监听就是最原始的监听!!!接下来到了重头戏了app
关于FloatingActionButton在项目中的使用,基本上就有如下这么多东西,这些都是我能想到的。若是你有什么好的应用效果能够和我分享一下:ide
简单的说一下实现方案:布局
implementation 'com.android.support:design:26.1.0'
25.1.0以前的版本中使用,若是在以后的版本中使用的话,通常隐藏了以后就不会出现了!!!)这里存在一个Behavior和CoordinatorLayout的概念,会在后期讲解,这里直接贴出相应的代码了!若是你想实现效果切记把com.android.support:design:XXX
设置成25.1.0之下的版本动画
public class ScrollAwareFABBehaviorDefault extends FloatingActionButton.Behavior {
public ScrollAwareFABBehaviorDefault(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, final View directTargetChild, final View target, final int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}
public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, final View target, final int dxConsumed, final int dyConsumed, final int dxUnconsumed, final int dyUnconsumed) {
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
child.hide();
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
child.show();
}
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
}
}
复制代码
以后在布局中设置一些behavior就能够了ui
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jinlong.newmaterialdesign.fab.FABAndRecyclerViewActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
app:layout_scrollFlags="scroll" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_content"
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="bottom|right"
android:layout_margin="30dp"
android:src="@mipmap/ic_call_missed_outgoing_white_24dp"
app:layout_behavior="com.jinlong.newmaterialdesign.fab.ScrollAwareFABBehaviorDefault" />
<!--layout_behavior设置的是控件的全路径-->
</android.support.design.widget.CoordinatorLayout>
复制代码
基本上就是上面这么多,我在网上找了很久,在25.1.0以后的版本,都是能够隐藏,可是怎么也显示不出来了。也没有找到很好的解决办法,因此这里我就换了一种解决方案,因此就有了下面这种解决方案。
mRvContent.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Log.e(TAG, "onScrolled: " + dy);
if (dy > 0 && mFab.getVisibility() == View.VISIBLE) {
//向下滑动,而且控件显示
mFab.hide();
} else if (dy < 0 && mFab.getVisibility() != View.VISIBLE) {
//向上滑动,而且控件为飞显示
mFab.show();
}
}
});
复制代码
上面的gif是用第二种方案实现的,因此关于上面选择那种方案,你们自行斟酌。。。
其实在Google推出FloatingActionButton以前,网上有不少大神都已经创造出来相应的组件了,这里介绍几个比较好的
其实关于FloatingActionButton的内容我总结的就这么多,但愿你们不要介意,还有一些内容是关系到CoordinatorLayout和Behavior的,由于内容比较多,我会在以后的文章单独讲解的,还但愿获得您的关注。。。。最后随口唠叨几句,程序员的成长在于不断的积累。天天进步一点点,总有一天咱们会成为码农的!!!!!!哈哈。。。