RecyclerView第一个项目的起始边距

35
如何在片段启动时将第一个项目与tvFoo对齐(而不将RecyclerView放入另一个容器中)?
现在,tvFoo与左边缘和列表中的第一个项目分开16dp,只有8dp。
我想要的是,在滚动时水平列表完全可见屏幕(但在开始时距离左侧为16dp)。
这就是我所拥有的: 这就是我想要的:

fragment_foo.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvFoo"
        style="@style/Tv.16.Black"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="@string/foo"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tvCount"
        style="@style/Tv.13.Gray"
        android:layout_marginStart="4dp"
        app:layout_constraintBottom_toBottomOf="@id/tvFoo"
        app:layout_constraintStart_toEndOf="@id/tvFoo"
        app:layout_constraintTop_toTopOf="@id/tvFoo"
        tools:text="(21)"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        style="@style/WrapContent"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toBottomOf="@id/tvFoo"
        tools:layoutManager="android.support.v7.widget.GridLayoutManager"
        tools:listitem="@layout/item_foo"
        tools:orientation="horizontal"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="16dp"
        android:background="@color/colorPrimaryLight"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/rv"/>
</android.support.constraint.ConstraintLayout>

item_foo.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    style="@style/WrapContent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_marginStart="8dp"
    android:background="?selectableItemBackground">

    <ImageView
        android:id="@+id/ivFoo"
        android:layout_width="160dp"
        android:layout_height="90dp"
        android:background="@color/lightGray"
        android:padding="16dp"
        android:src="@drawable/ic_picture_64dp"/>
</android.support.constraint.ConstraintLayout>

3
Gabriel,由于你的问题涉及UI设计,请用图片展示你目前拥有的内容和想要实现的目标。请不要改变原意,但需要让翻译更易懂,不提供其他信息。 - Alessio
这与左边框有关,@PriyankVadariya。 - Gabi Moreno
1
好的,根据@Alessio的评论,您能上传您想要的确切图片吗?这样我们可以帮助您。 - PriyankVadariya
谢谢你,@Alessio。完成了! - Gabi Moreno
1个回答

98
在你的RecyclerView中添加padding并使用

android:clipToPadding = "false"

来禁用滚动时的填充。请注意保留HTML标记。
<android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        style="@style/WrapContent"
        android:layout_marginTop="16dp"     
        android:padding="16dp"
        android:clipToPadding = "false"
        app:layout_constraintTop_toBottomOf="@id/tvFoo"
        tools:layoutManager="android.support.v7.widget.GridLayoutManager"
        tools:listitem="@layout/item_foo"
        tools:orientation="horizontal"/>

1
非常完美。唯一需要考虑的是将 android:padding="16dp" 更改为 android:padding="8dp",因为我在项目中使用了 android:layout_marginStart="8dp" - Gabi Moreno
4
我经常忘记 clipToPadding 属性... :-D - Gabi Moreno

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