如何使工具栏卡入视图或拿出来看使用谷歌设计库的时候?视图、工具栏、时候

2023-09-13 23:47:18 作者:碎了一地的真心

我想达到的效果像WhatsApp的都有,那里的工具栏(滚动时)将夹眼帘magnetlike,或拿出来看magnetlike。

I am trying to achieve an effect like WhatsApp has, where the Toolbar (when scrolled) will clip into view magnetlike, or out of view magnetlike.

我有我的即时通讯XML MainActivity:

What I have im my MainActivity XML:

DrawerLayout - 基地布局 CoordinatorLayout - 布局的Appbar和工具栏和标签 AppBarLayout - 用于固定工具栏和标签 工具栏 - 有这个标志:应用程序:layout_scrollFlags =滚动| enterAlways SlidingTabLayout - 显示卡 ViewPager - 对于标签页 RecyclerView - 对于coordinatorlayout DrawerLayout - Base Layout CoordinatorLayout - Layout for the Appbar and Toolbar and Tabs AppBarLayout - For holding Toolbar and Tabs Toolbar - has THIS flag: app:layout_scrollFlags="scroll|enterAlways" SlidingTabLayout - Displays tabs ViewPager - For tabs RecyclerView - For coordinatorlayout

现在不要误会我的意思,它的工作原理,当我向下滚动工具栏被排挤出的看法,但说我停止滚动一半,然后在工具栏只是坐在那里半隐半现拿出来看并考虑到另一半..

Now dont get me wrong, it works, when I scroll down the toolbar gets pushed out of view but say I stop scrolling halfway, then the toolbar just sits there half hidden out of view and the other half in view..

我如何能接近解决这一问题,因为我希望它要么振作起来视图或进入视野。

How can I approach solving this problem, as I want it to either snap out of view or into view.

推荐答案

编辑:作为支持23.1.0,这是不再需要。请参见这个答案代替。

as of support 23.1.0 this is no longer needed. See this answer instead.

要解决这个问题是定制的行为设置为一种可能的方式你的 AppBarLayout

One possible way to solve this is customizing the Behavior set to your AppBarLayout.

<android.support.design.widget.AppBarLayout
    app:layout_behavior="com.myapp.AppBarLayoutSnapBehavior"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    ...

AppBarLayoutSnapBehavior 将改变 AppBarLayout.Behavior 的默认行为,通过增加捕捉逻辑滚动停止时。 我们希望,code以下是自我解释。

Your AppBarLayoutSnapBehavior would change the default behavior of AppBarLayout.Behavior, by adding the snap logic when the scroll stops. Hopefully, the code below is self explanatory.

package com.myapp;

public class AppBarLayoutSnapBehavior extends AppBarLayout.Behavior {

    private ValueAnimator mAnimator;
    private boolean mNestedScrollStarted = false;

    public AppBarLayoutSnapBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
                                       View directTargetChild, View target, int nestedScrollAxes) {
        mNestedScrollStarted = super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
        if (mNestedScrollStarted && mAnimator != null) {
            mAnimator.cancel();
        }
        return mNestedScrollStarted;
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target) {
        super.onStopNestedScroll(coordinatorLayout, child, target);

        if (!mNestedScrollStarted) {
            return;
        }

        mNestedScrollStarted = false;

        int scrollRange = child.getTotalScrollRange();
        int topOffset = getTopAndBottomOffset();

        if (topOffset <= -scrollRange || topOffset >= 0) {
            // Already fully visible or fully invisible
            return;
        }

        if (topOffset < -(scrollRange / 2f)) {
            // Snap up (to fully invisible)
            animateOffsetTo(-scrollRange);
        } else {
            // Snap down (to fully visible)
            animateOffsetTo(0);
        }
    }

    private void animateOffsetTo(int offset) {
        if (mAnimator == null) {
            mAnimator = new ValueAnimator();
            mAnimator.setInterpolator(new DecelerateInterpolator());
            mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    setTopAndBottomOffset((int) animation.getAnimatedValue());
                }
            });
        } else {
            mAnimator.cancel();
        }

        mAnimator.setIntValues(getTopAndBottomOffset(), offset);
        mAnimator.start();
    }
}

唯一的一点是,滚动视图(在我的情况下, RecyclerView )卡连同工具栏。事实上,我喜欢这样,但我不知道这是你想要的。

The only thing is, the scroll view (in my case a RecyclerView) snaps along with the Toolbar. I actually like it this way, but I'm not sure that's what you want.