0

当 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;
}
}

recyclerview 物品少时如何处理?

4

1 回答 1

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();
        }
    }
}

RecyclerViewScrollListenerFAB 在滚动时隐藏,在空闲状态时显示。

于 2016-10-24T10:07:19.773 回答