以编程方式在RecyclerView上设置边距

17

我需要以编程方式设置RecyclerView的顶部边距,但是我遇到了以下异常:

java.lang.RuntimeException: Unable to resume activity java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams

这是我的代码:

RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams)recyclerView.getLayoutParams();
int marginTopDp = (int)getResources().getDimension(R.dimen.margin);
int marginTopPx = (int) (marginTopDp * getResources().getDisplayMetrics().density + 0.5f);
layoutParams.setMargins(0, marginTopPx, 0, 0);
recyclerView.setLayoutParams(layoutParams);

如果我按照堆栈跟踪所示使用 ViewGroup.LayoutParams layoutParams = recyclerView.getLayoutParams ,那么我就无法调用 setMargin 方法,因为该方法对于 ViewGroup.LayoutParams 不存在。

任何帮助将不胜感激。


你的RecyclerView的父ViewGroup是什么? - Bryan Dunlap
包含recyclerView的父级是android.support.v4.widget.SwipeRefreshLayout - VIN
将RecyclerView包装在类似LinearLayout的容器中,然后设置LinearLayout的边距如何? - TWL
虽然使用包装RecyclerView的方法可行,但为此添加额外的布局似乎有些浪费。我接受下面的答案,因为它可行且不需要添加仅用于包装目的的布局。 - VIN
TL;DR 从RecyclerView的父视图获取layoutParams - radus14
5个回答

26

尝试一下这个。您可以参考此处获取更多信息。

ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(mRecyclerView.getLayoutParams());
marginLayoutParams.setMargins(0, 10, 0, 10);
mRecyclerView.setLayoutParams(marginLayoutParams);

19
android.view.ViewGroup$LayoutParams 无法转换为 android.view.ViewGroup$MarginLayoutParams。 - Yazon2006
2
mRecyclerView.requestLayout(); 需要应用更改。 - satvinder singh

7

对于我来说,RecyclerView的父控件是一个ConstraintLayout

        val margins = (rv.layoutParams as ConstraintLayout.LayoutParams).apply {
        leftMargin = 0
        rightMargin = 0
    }
    rv.layoutParams = margins

否则会出现转换错误,viewGroup.LayoutParams 无法转换为 ConstraintLayout.LayoutParams。


1
你需要使用新的 MargingLayoutParams 对象。
    final FrameLayout.MarginLayoutParams marginLayoutParams = new      
                      FrameLayout.MarginLayoutParams(rvContacts.getLayoutParams());
    marginLayoutParams.leftMargin = left;
    marginLayoutParams.topMargin = top;
    marginLayoutParams.rightMargin = right;
    marginLayoutParams.bottomMargin = bottom;

    recyclerView.setLayoutParams(marginLayoutParams);
    recyclerView.requestLayout();

1

我也遇到了这种问题,使用下面的代码来解决recyclerview的边距问题。

    ViewGroup.MarginLayoutParams marginLayoutParams=
    (ViewGroup.MarginLayoutParams) recyclerView.getLayoutParams();

    // here i try to set only top margin, Get dimension from dimension file.
    int topMargin =  getResources()
    .getDimensionPixelSize(R.dimen.top_margin);

    marginLayoutParams.setMargins(marginLayoutParams.getMarginStart(),
    topMargin, marginLayoutParams.getMarginEnd(), marginLayoutParams.bottomMargin);

    recyclerView.setLayoutParams(marginLayoutParams);

0

在我的情况下,使用clipToPadding="false"填充使用起来很方便。[Kotlin]

所以我在xml中为我的RecyclerView添加了android:clipToPadding="false"。 也可以通过编程方式完成。

然后只需使用:

recyclerView.setPadding(10, 0, 0, someSpace)

someSpace是dp变量,如果需要的话。

resources.getDimension(R.dimen.some_space).toInt() P.S不要忘记在dimension文件中创建some_space :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接