國際慣例:效果圖(來自網絡,侵刪)html
其實主要用的就是CoordinatorLayout+Behaviorjava
首先自定義Behaviorandroid
- import android.animation.Animator;
- import android.content.Context;
- import android.support.annotation.NonNull;
- import android.support.design.widget.CoordinatorLayout;
- import android.support.v4.view.ViewCompat;
- import android.support.v4.view.animation.FastOutSlowInInterpolator;
- import android.util.AttributeSet;
- import android.view.View;
- import android.view.ViewPropertyAnimator;
- import android.view.animation.Interpolator;
-
-
-
-
-
-
-
- public class BottomBarBehavior extends CoordinatorLayout.Behavior<View> {
- private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
-
- private float viewY;
- private boolean isAnimate;
-
- public BottomBarBehavior(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- @Override
- public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int nestedScrollAxes, @ViewCompat.NestedScrollType int type) {
- if (child.getVisibility() == View.VISIBLE && viewY == 0) {
-
- viewY = coordinatorLayout.getHeight() - child.getY();
- }
-
-
-
- return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- @Override
- public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
- super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
-
-
- if (type == ViewCompat.TYPE_TOUCH) {
- if (dy > 20 && !isAnimate && child.getVisibility() == View.VISIBLE) {
- hide(child);
- } else if (dy < 20 && !isAnimate && child.getVisibility() == View.INVISIBLE) {
- show(child);
- }
- }
- }
-
-
- private void hide(final View view) {
- ViewPropertyAnimator animator = view.animate().translationY(viewY).setInterpolator(INTERPOLATOR).setDuration(500);
-
- animator.setListener(new Animator.AnimatorListener() {
- @Override
- public void onAnimationStart(Animator animator) {
- isAnimate = true;
- }
-
- @Override
- public void onAnimationEnd(Animator animator) {
- view.setVisibility(View.INVISIBLE);
- isAnimate = false;
- }
-
- @Override
- public void onAnimationCancel(Animator animator) {
- show(view);
- }
-
- @Override
- public void onAnimationRepeat(Animator animator) {
- }
- });
- animator.start();
- }
-
-
- private void show(final View view) {
- ViewPropertyAnimator animator = view.animate().translationY(0).setInterpolator(INTERPOLATOR).setDuration(500);
- animator.setListener(new Animator.AnimatorListener() {
- @Override
- public void onAnimationStart(Animator animator) {
- view.setVisibility(View.VISIBLE);
- isAnimate = true;
- }
-
- @Override
- public void onAnimationEnd(Animator animator) {
- isAnimate = false;
- }
-
- @Override
- public void onAnimationCancel(Animator animator) {
- hide(view);
- }
-
- @Override
- public void onAnimationRepeat(Animator animator) {
- }
- });
- animator.start();
- }
-
- }
自定義好了Behavior以後就須要在layout佈局文件裏使用了網絡
layout佈局:app
- <?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">
-
- <android.support.design.widget.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:fitsSystemWindows="true"
- android:minHeight="?attr/actionBarSize"
- android:theme="@style/AppTheme.AppBarOverlay">
-
- <android.support.v7.widget.Toolbar
- android:id="@+id/activity_dyeOrder_detail_toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- android:minHeight="?attr/actionBarSize"
- app:layout_scrollFlags="scroll|enterAlways"
- app:navigationIcon="@mipmap/ic_arrow_back_white_24dp"
- app:popupTheme="@style/AppTheme.PopupOverlay"
- app:title="@string/dyeOrderDetail" />
- </android.support.design.widget.AppBarLayout>
-
-
- <android.support.v4.widget.SwipeRefreshLayout
- android:id="@+id/swipeRefreshLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:layout_behavior="@string/appbar_scrolling_view_behavior">
-
- <android.support.v7.widget.RecyclerView
- android:id="@+id/dyeMessageList"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:descendantFocusability="beforeDescendants"
- android:scrollbars="vertical"
- tools:listitem="@layout/recycle_no_dye_message_entry_list" />
-
- </android.support.v4.widget.SwipeRefreshLayout>
-
- <LinearLayout
- android:id="@+id/addLayout"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:gravity="center"
- android:orientation="horizontal"
- android:background="@color/white"
- app:layout_behavior="BottomBarBehavior"
- app:layout_anchor="@id/swipeRefreshLayout"
- app:layout_anchorGravity="bottom|end">
- <TextView
- android:id="@+id/add"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:text="底部菜單" />
- </LinearLayout>
-
- </android.support.design.widget.CoordinatorLayout>
須要注意的是app:layout_behavior="BottomBarBehavior"中的BottomBarBehavior若是不在根目錄須要加上包名