【问题标题】:Set Top Padding affects Bottom Margin of a View设置顶部填充影响视图的底部边距
【发布时间】:2019-06-24 06:52:16
【问题描述】:

这是下边距:

@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 毫秒后出现,

但一旦应用了顶部填充,底部边距也会出现。为什么以及如何解决?

【问题讨论】:

  • 250ms 是一个非常短的时间来区分我认为。您是否在这里尝试了更长的时间并检查是否发生了同样的事情?
  • 是的,只要应用填充,底部边距也会出现。如果我为填充设置了 250 毫秒的延迟,那么它将按预期工作,但为什么呢?什么关系?
  • 我还是一头雾水。您是否延迟了 10 秒并检查是否有效?我认为 10 秒将是 10000 毫秒。请尝试使用它而不是 250 并在此处报告。
  • 即使我设置了 10 秒,它也会立即出现。您可以克隆项目并尝试:github.com/Ali-Rezaei/Contacts/tree/quick_hide_behaviour

标签: android android-coordinatorlayout


【解决方案1】:

您正在从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);
}

【讨论】:

  • 奇怪的是,如果我为顶部填充设置 250 毫秒延迟:'recyclerView.setPadding(0, actionBarHeight, 0, 0);'它按预期工作。这是什么原因?
  • 布局在设置内边距时触发。由于已设置边距,因此也将应用它们。在我的示例中,我添加了对 requestLayout() 的调用。
猜你喜欢
  • 1970-01-01
  • 2014-08-28
  • 2017-01-04
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 2014-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多