【问题标题】:CollapsedToolbar doesnt get open with fling折叠工具栏无法通过投掷打开
【发布时间】:2015-07-24 09:59:26
【问题描述】:

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

【问题讨论】:

  • 如果有人想知道我通过扩展滚动视图来修复它
  • 您介意详细说明一下解决方案吗?
  • 你为什么不发布一个答案?并且,请显示代码示例。

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


【解决方案1】:

如果有人想了解我是如何解决的,我会回答我自己的问题: 实际上它非常简单,我只听滚动视图是否过度滚动(检查 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 会使动画更流畅。

干杯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多