android - Recyclerview 第一项起始边距
问题描述
tvFoo
启动片段时如何将第一个项目对齐(不将其RecyclerView
放入另一个容器中)?
现在,tvFoo
距离左边缘 16dp 和列表中的第一项,只有 8dp。
我想要的是水平列表在滚动时在屏幕上完全可见(但一开始它距离左侧 16dp)。
片段_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>
解决方案
为您的和使用添加填充RecyclerView
android:clipToPadding = "false"
在你的RecyclerView
, 滚动时禁用填充。
<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"/>
推荐阅读
- python - Python:为什么用 re.IGNORECASE 编译的正则表达式会删除第一个字符?
- image - Vue Bootstrap img 未从资产文件夹中显示
- c - C:将矩形切成正方形的算法
- php - 使用 cURL 对 PHP 进行 C++ 身份验证
- android - 如果我的 minSdkVersion=21,如何使用没有支持/appcompact 库的工具栏?
- javascript - 如何允许 Vue 组件管理自己的转换?
- php - 如何分别获取键和值
- javascript - React native toLocaleDateString 在 Android 上的行为不同
- php - 查询指示不希望的事件
- sql - 在这种情况下如何使用 SUM?