How to animate FloatingActionButton like in Google+ app for Android?

前端 未结 3 1271
轮回少年
轮回少年 2020-12-23 10:49

I set FloatingActionButton to bottom of screen and I want to animate the button.

  • Hidden when scrolling down
  • Shown when scrolling up

Lik

3条回答
  •  别那么骄傲
    2020-12-23 11:32

    As of this post, there are no methods that will automatically handle hiding and showing the FloatingActionButton in the Design Support Libraries. I know this because this was my first assignment at work.

    The methods you are thinking of are used to animate the FloatingActionButton up and down when a Snackbar is created, and yes, that will work if you are using a CoordinatorLayout.

    Here's my code. It's based off of this repo. It has listeners for RecyclerView and AbsListView that handle animating the button automatically. You can either do

    button.show();
    

    or

    button.hide();
    

    to hide the button manually, or you can call:

    button.attachToListView(listView);
    

    and

    button.attachToRecyclerView(recyclerView);
    

    and it will hide on scroll down and show on scroll up with no further code.

    Hope this helps!

    AnimatedFloatingActionButton:

    public class AnimatedFloatingActionButton extends FloatingActionButton
    {
        private static final int TRANSLATE_DURATION_MILLIS = 200;
        private final Interpolator mInterpolator = new AccelerateDecelerateInterpolator();
        private boolean mVisible;
    
    public AnimatedFloatingActionButton(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        Log.i("Abscroll", "mVisible" + mVisible);
    }
    
    public void show() {
        show(true);
    }
    
    public void hide() {
        hide(true);
    }
    
    public void show(boolean animate) {
        toggle(true, animate, false);
    }
    
    public void hide(boolean animate) {
        toggle(false, animate, false);
    }
    
    private void toggle(final boolean visible, final boolean animate, boolean force) {
        if (mVisible != visible || force) {
            mVisible = visible;
            int height = getHeight();
            if (height == 0 && !force) {
                ViewTreeObserver vto = getViewTreeObserver();
                if (vto.isAlive()) {
                    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                        @Override
                        public boolean onPreDraw() {
                            ViewTreeObserver currentVto = getViewTreeObserver();
                            if (currentVto.isAlive()) {
                                currentVto.removeOnPreDrawListener(this);
                            }
                            toggle(visible, animate, true);
                            return true;
                        }
                    });
                    return;
                }
            }
            int translationY = visible ? 0 : height + getMarginBottom();
            Log.i("Abscroll", "transY" + translationY);
            if (animate) {
                this.animate().setInterpolator(mInterpolator)
                        .setDuration(TRANSLATE_DURATION_MILLIS)
                        .translationY(translationY);
            } else {
                setTranslationY(translationY);
            }
        }
    }
    
    private int getMarginBottom() {
        int marginBottom = 0;
        final ViewGroup.LayoutParams layoutParams = getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            marginBottom = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin;
        }
        return marginBottom;
    }
    
    public void attachToListView(@NonNull AbsListView listView)
    {
        listView.setOnScrollListener(new AbsListViewScrollDetector() {
            @Override
            void onScrollUp() {
                hide();
            }
    
            @Override
            void onScrollDown() {
                show();
            }
    
            @Override
            void setScrollThreshold() {
                setScrollThreshold(getResources().getDimensionPixelOffset(R.dimen.fab_scroll_threshold));
            }
        });
    }
    
    public void attachToRecyclerView(@NonNull RecyclerView recyclerView) {
        recyclerView.addOnScrollListener(new RecyclerViewScrollDetector() {
            @Override
            void onScrollUp() {
                hide();
            }
    
            @Override
            void onScrollDown() {
                show();
            }
    
            @Override
            void setScrollThreshold() {
                setScrollThreshold(getResources().getDimensionPixelOffset(R.dimen.fab_scroll_threshold));
            }
        });
    }
    }
    

    AbsListViewScrollDetector:

    abstract class AbsListViewScrollDetector implements AbsListView.OnScrollListener {
    private int mLastScrollY;
    private int mPreviousFirstVisibleItem;
    private AbsListView mListView;
    private int mScrollThreshold;
    
    abstract void onScrollUp();
    
    abstract void onScrollDown();
    
    abstract void setScrollThreshold();
    
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }
    
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if(totalItemCount == 0) return;
        if (isSameRow(firstVisibleItem)) {
            int newScrollY = getTopItemScrollY();
            boolean isSignificantDelta = Math.abs(mLastScrollY - newScrollY) > mScrollThreshold;
            Log.i("Abscroll", "mLastScrollY " + mLastScrollY);
            Log.i("Abscroll", "newScrollY " + newScrollY);
            if (isSignificantDelta) {
                Log.i("Abscroll", "sig delta");
                if (mLastScrollY > newScrollY) {
                    onScrollUp();
                    Log.i("Abscroll", "sig delta up");
                } else {
                    onScrollDown();
                    Log.i("Abscroll", "sig delta down");
                }
            }
            mLastScrollY = newScrollY;
        } else {
            if (firstVisibleItem > mPreviousFirstVisibleItem) {
                onScrollUp();
                Log.i("Abscroll", "prev up");
            } else {
                onScrollDown();
                Log.i("Abscroll", "prev down");
            }
    
            mLastScrollY = getTopItemScrollY();
            mPreviousFirstVisibleItem = firstVisibleItem;
        }
    }
    
    public void setScrollThreshold(int scrollThreshold) {
        mScrollThreshold = scrollThreshold;
        Log.i("Abscroll", "LView thresh " + scrollThreshold);
    }
    
    public void setListView(@NonNull AbsListView listView) {
        mListView = listView;
    }
    
    private boolean isSameRow(int firstVisibleItem) {
        return firstVisibleItem == mPreviousFirstVisibleItem;
    }
    
    private int getTopItemScrollY() {
        if (mListView == null || mListView.getChildAt(0) == null) return 0;
        View topChild = mListView.getChildAt(0);
        return topChild.getTop();
    }
    }
    

    RecyclerViewScrollDetector:

    abstract class RecyclerViewScrollDetector extends RecyclerView.OnScrollListener {
    private int mScrollThreshold;
    
    abstract void onScrollUp();
    
    abstract void onScrollDown();
    
    abstract void setScrollThreshold();
    
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        boolean isSignificantDelta = Math.abs(dy) > mScrollThreshold;
        if (isSignificantDelta) {
            if (dy > 0) {
                onScrollUp();
                Log.i("Abscroll", "Rview up");
            } else {
                onScrollDown();
                Log.i("Abscroll", "RView down");
            }
        }
    }
    
    public void setScrollThreshold(int scrollThreshold) {
        mScrollThreshold = scrollThreshold;
        Log.i("Abscroll", "RView thresh " + scrollThreshold);
    }
    }
    

提交回复
热议问题