Home > Blockchain >  in kotlin why is my recyclerview not scrolling
in kotlin why is my recyclerview not scrolling

Time:04-02

I am new to recycler views and beginning with Room databases. I am working on this app that lets the user enter records into Room database. The user enters the info and it gets stored. The top part of the screen is the data entry and the bottom is the recycler view where the records are displayed. Everything is fine until there are enough records to fill the available space. Then the new records just disappear below the screen. There is animation when the user tries to scroll, but the items don't move. I tried to make the items bigger by adding a padding, and strangely enough there is scrolling now, but just to the same item that displayed before.

This is the main.xml

<?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="match_parent"
                                                   android:layout_height="match_parent"
                                                   android:padding="10dp"
                                                   tools:context=".MainActivity">

    <TextView
            android:id="@ id/tvNameLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            app:layout_constraintBottom_toBottomOf="@id/etName"
            app:layout_constraintTop_toTopOf="@ id/etName"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@ id/etName"

    />

    <EditText
            android:id="@ id/etName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="Enter Name"
            android:layout_marginStart="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@ id/tvNameLabel"/>

    <TextView
            android:id="@ id/tvEmailLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Email Id"
            app:layout_constraintBottom_toBottomOf="@ id/etMailId"
            app:layout_constraintEnd_toStartOf="@ id/etMailId"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/etName"/>

    <EditText
            android:id="@ id/etMailId"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="Enter Email Id"
            android:layout_marginStart="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@ id/tvEmailLabel"
            app:layout_constraintTop_toBottomOf="@ id/etName"/>

    <Button
            android:id="@ id/btnAdd"
            android:layout_width="match_parent"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_height="80dp"
            android:text="ADD RECORD"
            app:layout_constraintTop_toBottomOf="@ id/etMailId"/>

    <TextView
            android:id="@ id/tvName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:gravity="center"
            android:text="All The Inserted Records"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintTop_toBottomOf="@ id/btnAdd"/>

    <androidx.recyclerview.widget.RecyclerView
            android:id="@ id/rvItemsList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/tvName"
            android:visibility="visible"
            tools:visibility="visible"
    />

    <TextView
            android:id="@ id/tvNoRecordsAvailable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/btnAdd"
            android:gravity="center"
            android:textSize="18sp"
            android:textColor="#997b66"
            android:text="There Are No records"
            tools:visibility="visible"/>


</androidx.constraintlayout.widget.ConstraintLayout>

and this is the items xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@ id/llMain"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:gravity="center_vertical"
              android:padding="20dp">

    <TextView
            android:id="@ id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:text="Name"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:textSize="18sp"
            android:textStyle="bold"
            android:text=":"/>

    <TextView
            android:id="@ id/tvEmail"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:text="Email"/>

    <ImageView
            android:id="@ id/ivEdit"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:contentDescription="image"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:scaleType="center"
            android:src="@drawable/ic_action_edit"/>

    <ImageView
            android:id="@ id/ivDelete"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:contentDescription="image"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:scaleType="center"
            android:src="@drawable/ic_action_delete"/>

</LinearLayout>

I don't know if any of the other files could be the culprits; but I can add them.

Any help would be greatly appreciated.

CodePudding user response:

Add bottom constraint to recycler, then 0dp to height

CodePudding user response:

I think you need to two things.

  1. Try making the Recycler View in the main.xml file as big vertically as possible. For example android:layout_height="0dp"
  2. I think you should add constraints to the Recycler View. Something like app:layout_constraintStart_toStartOf="parent" and app:layout_constraintEnd_toEndOf="parent"

CodePudding user response:

Your RecyclerView is set with a height of wrap_content - that makes the size of the widget enough to display all its contents. It won't need to scroll, because it's big enough to display everything!

The problem there is you can't see the whole widget, because it's so tall the bottom is off the screen at this point. For scrollable containers, you usually want to constrain their height (or width if it's a horizontal scroll) to give you a "window" into the contents. If the contents are too big to fit in that window, the container will scroll behind that window.

In your case, you probably want to constrain the bottom of your RecyclerView to the bottom of the screen, or maybe the top of that TextView that sits below it:

android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/tvName"
app:layout_constraintBottom_toBottomOf="parent"

That defines the area that the list takes up, as the available space between tvName and the bottom of the layout. 0dp means it has no absolute height, and it'll just size to fit those vertical constraints. You can be a bit more clever with it, but that's the basic idea - define an area for your list to use, and if it's not big enough for the contents, the list will scroll

  • Related