【问题标题】:Android - RecyclerView list equal spacing between items and parentAndroid - RecyclerView 列表的项目和父项之间的间距相等
【发布时间】:2020-04-10 04:32:01
【问题描述】:

我无法弄清楚如何在 XML 文件中为我的回收站视图设置填充和边距,以实现此行为,保持回收站列表项之间的间距相等,并且与父级的间距相同。

ParenStart ListItem ListItem ParentEnd

并且同时能够滚动整个屏幕宽度(父级)。

问题是,如果我在父项和列表项上设置填充,就会发生这种行为,如下所示: ParenStart ListItem ListItem ParentEnd

如果我没有在父项上设置填充,而只在列表项上设置填充,我会遇到以下问题 ParenStart ListItem ListItem ParentEnd

重申我的目标: ParenStart ListItem ListItem ParentEnd 并且同时能够滚动父级的全宽。

【问题讨论】:

  • 除了recyclerview之外,您可以尝试为父视图内的所有视图提供填充,而不是为整个父视图提供填充

标签: android xml android-recyclerview


【解决方案1】:

您可以在RecyclerView 中使用clipToPadding 属性。

<androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingStart="10dp"
             />

然后在项目布局中仅使用右边距。然后安排就像
ParentStart(clippadding) &lt;10dp&gt; ListItem &lt;10dp&gt; ListItem &lt;10dp&gt; ParentEnd.

或者您可以尝试使用DividerItemDecoration ..

【讨论】:

    【解决方案2】:

    在recyclerview中添加如下clipToPadding

     <android.support.v7.widget.RecyclerView
                    android:id="@+id/rv_tpf"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clipToPadding="false"
                    android:paddingStart="10dp" />
    

    添加android:clipToPadding="false".

    【讨论】:

      猜你喜欢
      • 2015-10-08
      • 2015-01-20
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      相关资源
      最近更新 更多