设置顶部填充会影响视图的底部边距

Set Top Padding affects Bottom Margin of a View

这是底边距:

@Override
protected void directionDownScrolling(View recyclerView) {
    MarginLayoutParams params = (MarginLayoutParams) recyclerView.getLayoutParams();
    params.setMargins(0, 0, 0,
            (int) recyclerView.getContext().getResources().getDimension(R.dimen.dimen_recycler_view_spacing));
    mHandler.postDelayed(() -> recyclerView.setLayoutParams(params), 250);
}

顶部内边距:

@Override
protected void directionDownScrolling(View recyclerView) {
    // Calculate ActionBar height
    TypedValue tv = new TypedValue();
    int actionBarHeight = recyclerView.getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true) ?
            TypedValue.complexToDimensionPixelSize(tv.data, recyclerView.getContext().getResources().getDisplayMetrics()) :
            (int) recyclerView.getContext().getResources().getDimension(R.dimen.dimen_recycler_view_spacing);
    recyclerView.setPadding(0, actionBarHeight, 0, 0);
}

如您所见,顶部填充立即应用,但我预计底部边距会在 250 毫秒后出现,

但是一旦顶部填充应用,底部边距也会出现。为什么以及如何修复它?

您正在从 recyclerView:

获取布局参数
MarginLayoutParams params = (MarginLayoutParams) recyclerView.getLayoutParams();

然后你直接在上面设置边距:

params.setMargins(0, 0, 0,
        (int) recyclerView.getContext().getResources().getDimension(R.dimen.dimen_recycler_view_spacing));

所以延迟消息没有做任何事情:

mHandler.postDelayed(() -> recyclerView.setLayoutParams(params), 250);

如果您将布局参数设置为其他一些实例,这将会有所不同。而是调用 setMargins 延迟:

@Override
protected void directionDownScrolling(View recyclerView) {
    MarginLayoutParams params = (MarginLayoutParams) recyclerView.getLayoutParams();
    int marginBottom = (int) recyclerView.getContext().getResources().getDimension(R.dimen.dimen_recycler_view_spacing));
    mHandler.postDelayed(() -> {
            params.setMargins(0, 0, 0, marginBottom);
            recyclerView.requestLayout();
    }, 250);
}