CollapsedToolbar无法打开

时间:2015-07-24 09:59:27

标签: android material-design android-design-library

CollapsedToolbar doesnt get open with fling

这是一个不言自明的问题。我无法使用折叠工具栏在nestedScrollView中使用fling手势进行扩展。在投掷手势(注意到过度)后,我手动滚动到顶部以使工具栏扩展。我尝试了滚动标记的一些变体,如scroll | enterAlwaysCollapsed等。 有没有什么办法解决这一问题 ?

1 个答案:

答案 0 :(得分:1)

我回答我自己的问题,如果有人想了解我是如何解决它的: 实际上它非常简单,我只是听看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);
    }
}

在活动中设置监听器

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”可以使动画更加流畅。

干杯。