Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recyclerview first item start margin

How do I align the first item to tvFoo when starting the fragment (without putting theRecyclerView inside another container)?

Right now, tvFoo is separated 16dp from the left edge and the first item from the list, only 8dp.

What I want is that the horizontal list is visible completely across the screen when scrolling (but at the beginning it is 16dp from the left).

This is what I have

This is what I want

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>
like image 585
Gabi Moreno Avatar asked Aug 22 '18 06:08

Gabi Moreno


1 Answers

Add padding to your RecyclerView and Use

android:clipToPadding = "false"

inside your RecyclerView, to disable padding while scrolling it.

<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"/>
like image 90
Yamini Balakrishnan Avatar answered Oct 23 '22 07:10

Yamini Balakrishnan