Home » Android » android – Cannot fully scroll to first and last items with padding on both ends in recycler view-Exceptionshub

android – Cannot fully scroll to first and last items with padding on both ends in recycler view-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have recycler view in a home fragment.

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewSkills"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:clipToPadding="false"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3" />

The Item in the recycler view has layout as follows:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:padding="4dp">

        <androidx.cardview.widget.CardView
                android:id="@id/cardView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:cardElevation="4dp"
                android:clipToPadding="false"
                app:cardCornerRadius="8dp"
                app:contentPaddingLeft="48dp"
                app:contentPaddingRight="48dp"
                app:contentPaddingTop="24dp"
                app:contentPaddingBottom="24dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

            <ImageView
                    android:id="@+id/imageViewCategoryIcon"
                    android:layout_width="@dimen/category_card_icon_size_small"
                    android:layout_height="@dimen/category_card_icon_size_small"
                    android:adjustViewBounds="true" />

        </androidx.cardview.widget.CardView>

    <TextView
        android:id="@+id/textViewCategoryName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="2dp"
        android:ellipsize="end"
        android:gravity="center"
        android:lines="2"
        android:textAlignment="center"
        android:textColor="@color/colorPrimary"
        app:layout_constraintEnd_toEndOf="@+id/cardView"
        app:layout_constraintStart_toStartOf="@+id/cardView"
        app:layout_constraintTop_toBottomOf="@+id/cardView"
        tools:text="Communication" />

    <ImageView
            android:id="@+id/imageViewPremium"
            android:elevation="8dp"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@drawable/ribbon"
            android:visibility="gone"
            app:layout_constraintTop_toTopOf="@id/cardView"
            app:layout_constraintRight_toRightOf="@id/cardView"
            tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>

My problem is when I cannot fully scroll to the first or last item.

When I scroll to the end it looks like image below:

enter image description here

But when I leave it, it goes back to the position as shown below: Somehow when scroll horizontal the recycler view’s padding on left and right doesn’t work.

enter image description here

How to&Answers:

Setting both height and width of RecyclerView to 0dp worked for me. (when using constraintLayout)

Answer:

I think u should use margin instead of padding in your recyclerview :

Change your recyclerview to this :

  <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewSkills"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:clipToPadding="false"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />