如何使用 CoordinatorLayout.Behavior 检测嵌套投掷是否完全停止?当回收站视图完全停止时,没有这样的 api 可以给我回调。
2 回答
当RecyclerView正在滚动时,我试图隐藏浮动操作按钮(FAB) 时,我从这个兔子洞开始。根据多个来源执行此操作的正确方法是扩展FloatingActionButton.Behavior,覆盖and方法,并将您的行为与 FAB 挂钩,例如. 这适用于正常(慢速)滚动事件,但不会在投掷结束时调用。 onStartNestedScroll
onStopNestedScroll
app:layout_behavior="com.justingarrick.ui.ScrollAwareFabBehavior"
onStopNestedScroll
目前,似乎有一些关于投掷和滚动行为的未解决问题;我的解决方法是为我的 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()
方法,当您尝试在 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);
}
});
}
我使用 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;
}
}
}