【问题标题】:Set Margin on RecyclerView programmatically以编程方式在 RecyclerView 上设置边距
【发布时间】:2016-12-23 10:41:12
【问题描述】:

我需要以编程方式设置 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 是什么?
  • 持有recyclerView的父级是android.support.v4.widget.SwipeRefreshLayout
  • 如何将 recyclerview 包裹在类似线性布局的东西中,然后为线性布局设置边距?
  • 虽然包装 recyclerView 有效,但为此目的添加额外的布局似乎很浪费。我接受了下面的答案,因为它可以工作并且不涉及仅为包装目的添加布局。

标签: android android-recyclerview layoutparams


【解决方案1】:

试试这个。您可以参考this了解更多信息。

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

【讨论】:

  • android.view.ViewGroup$LayoutParams 无法转换为 android.view.ViewGroup$MarginLayoutParams
  • mRecyclerView.requestLayout();需要应用更改
【解决方案2】:

你需要使用 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();

【讨论】:

    【解决方案3】:

    对我来说,recyclerview 的父级是 constraintLayout

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

    否则会出现转换错误,viewGroup.LayoutParams 不能转换为 COnstarintLayout.LayoutParams

    【讨论】:

      【解决方案4】:

      我也遇到这种问题,使用下面的代码来处理recyclerview的margin问题。

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

      【讨论】:

        【解决方案5】:

        在我的情况下,使用 clipToPadding="false" 进行填充可以解决问题。 [科特林]

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

        然后就用了:

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

        someSpace 是 dp 变量,以备不时之需。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-29
          • 2019-10-19
          • 2023-04-01
          • 1970-01-01
          • 2014-11-26
          • 1970-01-01
          • 1970-01-01
          • 2015-02-24
          相关资源
          最近更新 更多