如何检测CoordinatorLayout.Behavior中停止的嵌套fling?

时间:2015-06-23 10:40:22

标签: android android-support-library android-coordinatorlayout

如何使用CoordinatorLayout.Behavior检测嵌套的fling是否完全停止? 当回收者视线完全停止时,没有这样的api可以给我回调。

2 个答案:

答案 0 :(得分:6)

当我在滚动Floating Action Button时试图隐藏RecyclerView(FAB)时,我开始关注这个兔子洞。根据多个sources执行此操作的正确的方法是扩展FloatingActionButton.Behavior,覆盖onStartNestedScrollonStopNestedScroll方法,并将您的行为挂钩到FAB,例如app:layout_behavior="com.justingarrick.ui.ScrollAwareFabBehavior"。这适用于正常(慢)滚动事件,但是当fling结束时不会调用onStopNestedScroll

目前,似乎有一些开放式issues具有投掷和滚动行为;对我来说,解决方法是为我的RecyclerView实现OnScrollListener,并且只是以编程方式更改FAB的状态,例如:

public class MyFragment extends Fragment {

    @Bind(R.id.account_list) RecyclerView recyclerView;
    @Bind(R.id.button_fab) FloatingActionButton fab;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_accounts, container, false);
        ButterKnife.bind(this, view);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING)
                    fab.hide(); // or hideFab(), see below
                else if (newState == RecyclerView.SCROLL_STATE_IDLE)
                    fab.show(); // or showFab(), see below
            }
        });

        return view;
    }
}

更新:这在99%的情况下都能正常运行,但如果您使用设计库22.2.1版中的show()hide()方法,则可以&# 39;当您尝试向上滚动RecyclerView的顶部或向下滚动到RecyclerView的底部时会遇到问题,因为回收者视图正在将状态从RecyclerView.SCROLL_STATE_DRAGGING切换到RecyclerView.SCROLL_STATE_IDLE这么快就会创建FloatingActionButtonHoneycombMr1#show()中的竞争条件。因此,要解决此问题(叹气),如果您不关心动画,则需要切换到setVisibility()来电,或者在没有竞争条件的情况下重新实现动画,例如。

private void hideFab() {
     fab.animate().scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setDuration(200L).setInterpolator(new FastOutSlowInInterpolator()).setListener(new AnimatorListenerAdapter() {
         @Override
         public void onAnimationEnd(Animator animation) {
             fab.setVisibility(View.GONE);
         }
     });
 }

 private void showFab() {
     fab.animate().scaleX(1.0F).scaleY(1.0F).alpha(1.0F).setDuration(200L).setInterpolator(new FastOutSlowInInterpolator()).setListener(new AnimatorListenerAdapter() {
         @Override
         public void onAnimationStart(Animator animation) {
            fab.setVisibility(View.VISIBLE);
         }
     });
 }

答案 1 :(得分:0)

我使用ScrollerCompat和方法ScrollerCompat.fling(..)来处理滚动的中间状态。所以我知道滚动结束的时候。

@Override
public boolean onNestedFling(final CoordinatorLayout coordinatorLayout, final AppBarLayout child, final View target, final float velocityX, final float velocityY, final boolean consumed) {
    final int scrollY = target.getScrollY();
    final double distance = mFlingHelper.getSplineFlingDistance(velocityY);

    fling(
            child,
            (int) distance + scrollY,
            velocityY,
            scrollY);

    return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
}

private void fling(final View pChild, final int pMaxOffset, final float pVelocityY, final int pStartY) {
    stopFling(pChild);
    mIsFlingRunning = true;

    if (mScroller == null) {
        mScroller = ScrollerCompat.create(mContext);
    }

    mScroller.fling(
        0, pStartY, // current
        0, Math.round(pVelocityY), // velocity.
        0, 0, // x
        0, pMaxOffset); // y

    if (mScroller.computeScrollOffset()) {
        mFlingRunnable = new FlingRunnable(pChild);
        ViewCompat.postOnAnimation(pChild, mFlingRunnable);
    }

}

private void stopFling(final View pChild) {
    if (mFlingRunnable != null) {
        pChild.removeCallbacks(mFlingRunnable);
        mFlingRunnable = null;
    }
}




private class FlingRunnable implements Runnable {

     private final View mView;

     FlingRunnable(final View pView) {
         mView = pView;
     }

     @Override
     public void run() {
         if (mView != null && mScroller != null) {
             if (mScroller.computeScrollOffset()) {
                 mIsFlingRunning = true;
                 // Post ourselves so that we run on the next animation
                 ViewCompat.postOnAnimation(mAppBarLayout, this);
             }
         } else {
             mIsFlingRunning = false;
         }
     }
 }