折叠工具栏无法通过 fling 打开

CollapsedToolbar doesnt get open with fling

这是一个不言自明的问题。我无法在 nestedScrollView 中使用滑动手势展开折叠的工具栏。在挥动手势(注意溢出)后,我手动滚动到顶部以使工具栏展开。我尝试了滚动标志的一些变体,例如 scroll|enterAlwaysCollapsed 等。 有没有什么办法解决这一问题 ?

如果有人想了解我是如何解决它的,我会回答我自己的问题: 实际上它非常简单,我只听 scrollview 是否过度滚动(检查 gif 上的发光)并抛出一个事件。我也在检查用户是否仍在触摸,这样它就不会突然展开

public class NestedScrollViewFling extends NestedScrollView
{
    private OnFlingEndReachedTopListener mListener;
    private Boolean isBeingTouched = false;

    public NestedScrollViewFling(Context context)
    {
        super(context);
    }

    public NestedScrollViewFling(Context context, AttributeSet attrs,
                                 int defStyle)
    {
        super(context, attrs, defStyle);
    }

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

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        int action = ev.getAction();

        if ((action == MotionEvent.ACTION_DOWN) || (action == MotionEvent.ACTION_MOVE))
            isBeingTouched = true;
        else
            isBeingTouched = false;

        return super.onTouchEvent(ev);
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY)
    {
        if (clampedY && scrollY == 0)
        {
            if (mListener != null)
                mListener.onTopReached(isBeingTouched);
        }

        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    }

    public OnFlingEndReachedTopListener getOnTopReachedListener()
    {
        return mListener;
    }

    public void setOnTopReachedListener(
            OnFlingEndReachedTopListener onTopReachedListener)
    {
        mListener = onTopReachedListener;
    }

    /**
     * Event listener.
     */
    public interface OnFlingEndReachedTopListener
    {
        public void onTopReached(Boolean isBeingTouch);
    }
}

并在 activity 上设置监听器

scrollView.setOnTopReachedListener(new NestedScrollViewFling.OnFlingEndReachedTopListener()
{
    @Override
    public void onTopReached(Boolean isBeingTouched)
    {
        if (!isBeingTouched)
            expandToolbar();
    }
});

最后是 expandToolbar 方法和处理程序

public void expandToolbar()
{
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
    if (behavior != null && !stop)
    {
        behavior.onNestedFling(coordinatorLayout, appbarLayout, null, 0, -10000, false);
        stop = true;
        useHandler();
    }
}

Handler mHandler;
Boolean stop = false;

public void useHandler()
{
    mHandler = new Handler();
    mHandler.postDelayed(mRunnable, 200);
}

private Runnable mRunnable = new Runnable()
{
    @Override
    public void run()
    {
        stop = false;
    }
};

在试验我的代码时,我发现将 expandToolbar 方法中的 -10000 值更改为 "-appbarLayout.getHeight() * 5 会使动画更流畅。

干杯。