Android -FAB行为有半个列表

时间:2016-10-24 09:41:35

标签: android floating-action-button android-nestedscrollview

当recyclerview有足够的项目可以滚动时,我有FAB工作,但是当recyclerview没有滚动时,我需要处理这个案例(项目总数不会覆盖屏幕)。

目前这就是我处理卷轴的方式:

public class FABBehavior extends FloatingActionButton.Behavior {

public FABBehavior() {
    super();
}

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

@Override
public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, final View target, final int dxConsumed, final int dyConsumed, final int dxUnconsumed, final int dyUnconsumed) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

    if (dyConsumed > 0) {
        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
        int fab_bottomMargin = layoutParams.bottomMargin;
        child.animate().translationY(child.getHeight() + fab_bottomMargin).setInterpolator(new LinearInterpolator()).start();
    } else if (dyConsumed < 0) {
        child.animate().translationY(0).setInterpolator(new LinearInterpolator()).start();
    }
}

@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, final View directTargetChild, final View target, final int nestedScrollAxes) {
    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
}

如何处理recyclelerview的物品很少?

1 个答案:

答案 0 :(得分:1)

您必须独立于CoordinatorLayout处理另一个案例。

覆盖函数layoutDependsOn

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
    return super.layoutDependsOn(parent, child, dependency) || dependency instanceof RecyclerView;
}

onNestedScroll还应该处理另一个案例:

if (target instanceof RecyclerView) {
    handleRecyclerViewScrolling(target, child);
    return;
}

handleRecyclerViewScrolling应如下所示:

private void handleRecyclerViewScrolling(View target, FloatingActionButton child) {
    if (scrollListener != null) {
        return;
    }
    RecyclerView recyclerView = (RecyclerView) target;
    scrollListener = new RecyclerViewScrollListener(child);
    recyclerView.addOnScrollListener(scrollListener);
}

scrollListener应该是FABBehavior课程中的字段。同时在FABBehavior

中声明内部类
private class RecyclerViewScrollListener extends RecyclerView.OnScrollListener {
    FloatingActionButton mChild;

    public RecyclerViewScrollListener(FloatingActionButton child) {
        this.mChild = child;
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            mChild.show();
        } else {
            mChild.hide();
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (!recyclerView.canScrollVertically(Integer.MAX_VALUE)) {
            mChild.show();
        }
    }
}

RecyclerViewScrollListener隐藏FAB,当它滚动时显示它并处于空闲状态。