Think_Twice
Think_Twice

Reputation: 323

Android RecyclerView inside a RelativeLayout not scrolling properly

I am making a android app that has an activity where the user selects how many random numbers to be generated and then displays the numbers. This is all inside a RelativelLayout that has a ScrollView inside it. In the scroll view thers is a static CardView in which the user selects the numbers they want and then they click a button that will generate and display it in the RecyclerView (dynamically creates the same number of CardView selected by the user.) below the static CardView. I have it all working right except for the scrolling and spacing between the CardView added dynamically. The CardViews in the Recylerview are somehow being spaced really big.

Below is a screenshot of the issue:

Below is a screenshot of the issue:

My CardView Item Layout Code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/LottoNumberGenerator_Numbers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/LottoNumberGenerator_NumbersHeading"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toStartOf="@+id/LottoNumberGenerator_Menu"
                android:gravity="center_horizontal"
                android:text="Number Combination #1"
                android:textAppearance="@style/TextAppearance.AppCompat.Large"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/LottoNumberGenerator_Menu"
                android:layout_width="20dp"
                android:layout_height="30dp"
                android:layout_alignParentRight="true"
                android:layout_margin="5dp"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_dots"/>

            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/LottoNumberGenerator_Menu">

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/LottoNumberGenerator_Number1"
                        android:layout_width="0dp"
                        android:layout_height="50dp"
                        android:layout_margin="2dp"
                        android:layout_weight="1"
                        android:background="@drawable/number_red"
                        android:gravity="center"
                        android:text="10"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/LottoNumberGenerator_Number2"
                        android:layout_width="0dp"
                        android:layout_height="50dp"
                        android:layout_margin="2dp"
                        android:layout_weight="1"
                        android:background="@drawable/number_green"
                        android:gravity="center"
                        android:text="30"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/LottoNumberGenerator_Number3"
                        android:layout_width="0dp"
                        android:layout_height="50dp"
                        android:layout_margin="2dp"
                        android:layout_weight="1"
                        android:background="@drawable/number_blue"
                        android:gravity="center"
                        android:text="50"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/LottoNumberGenerator_Number4"
                        android:layout_width="0dp"
                        android:layout_height="50dp"
                        android:layout_margin="2dp"
                        android:layout_weight="1"
                        android:background="@drawable/number_yellow"
                        android:gravity="center"
                        android:text="70"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/LottoNumberGenerator_Number5"
                        android:layout_width="0dp"
                        android:layout_height="50dp"
                        android:layout_margin="2dp"
                        android:layout_weight="1"
                        android:background="@drawable/number_red"
                        android:gravity="center"
                        android:text="90"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/LottoNumberGenerator_Number6"
                        android:layout_width="0dp"
                        android:layout_height="50dp"
                        android:layout_margin="2dp"
                        android:layout_weight="1"
                        android:background="@drawable/number_yellow"
                        android:gravity="center"
                        android:text="99"
                        android:textSize="24sp"
                        android:textStyle="bold" />
                </TableRow>

            </TableLayout>

            <Space
                android:layout_width="1dp"
                android:layout_height="10dp" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>

My MainActivity Code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_lotto_number_generator">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/Generator_BannerAd"
        android:layout_alignParentStart="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_marginBottom="10dp">

            <android.support.v7.widget.CardView
                android:id="@+id/Generator_Lines"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="5dp"
                card_view:cardCornerRadius="2dp"
                card_view:contentPadding="10dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/Generator_LinesHeading"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:text="Numbers Wanted?"
                        android:textAppearance="@style/TextAppearance.AppCompat.Large"
                        android:textColor="@color/colorPrimary"
                        android:textStyle="bold" />

                    <TableLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/Generator_LinesHeading">

                        <TableRow
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <Button
                                android:id="@+id/Generator_LinesMinus"
                                android:layout_width="0dp"
                                android:layout_height="100dp"
                                android:layout_gravity="center_vertical|center_horizontal"
                                android:layout_margin="5dp"
                                android:layout_weight="1"
                                android:background="@drawable/button"
                                android:gravity="center_vertical"
                                android:padding="0dp"
                                android:text="-"
                                android:textAlignment="center"
                                android:textColor="@color/colorPrimaryDark"
                                android:textSize="55sp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/Generator_LinesAmount"
                                android:layout_width="0dp"
                                android:layout_height="match_parent"
                                android:layout_margin="5dp"
                                android:layout_weight="1"
                                android:gravity="center_vertical|center_horizontal"
                                android:text="1"
                                android:textAlignment="center"
                                android:textColor="@color/colorPrimaryDark"
                                android:textSize="60sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/Generator_LinsPlus"
                                android:layout_width="0dp"
                                android:layout_height="100dp"
                                android:layout_gravity="center_vertical|center_horizontal"
                                android:layout_margin="5dp"
                                android:layout_weight="1"
                                android:background="@drawable/button"
                                android:gravity="center_vertical"
                                android:padding="0dp"
                                android:text="+"
                                android:textAlignment="center"
                                android:textColor="@color/colorPrimaryDark"
                                android:textSize="55sp"
                                android:textStyle="bold" />

                        </TableRow>

                        <TableRow>

                            <Button
                                android:id="@+id/Generator_Generate"
                                android:layout_width="0dp"
                                android:layout_height="50dp"
                                android:layout_margin="5dp"
                                android:layout_weight="1"
                                android:background="@drawable/button"
                                android:text="Generate Numbers"
                                android:textColor="@color/colorPrimaryDark"
                                android:textSize="18sp" />

                        </TableRow>

                    </TableLayout>

                    <Space
                        android:layout_width="1dp"
                        android:layout_height="10dp" />

                </RelativeLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/Generator_RecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/Generator_Lines"
                android:scrollbars="none">

            </android.support.v7.widget.RecyclerView>

        </RelativeLayout>

    </ScrollView>

    <LinearLayout
        android:id="@+id/Generator_BannerAd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        android:layout_marginTop="5dp"></LinearLayout>

</RelativeLayout>

Upvotes: 0

Views: 2037

Answers (2)

dz00dz
dz00dz

Reputation: 172

In the line 6 of your item layout replace

android:layout_height="match_parent"

with

android:layout_height="wrap_content"

Upvotes: 1

Matthew Vanlandingham
Matthew Vanlandingham

Reputation: 646

It may be very simple. What I think is happening is that since the RelativeLayout in your CardView has a height of match_parent, it's matching the height of the cardview with the height of the overall layout. If you change this to wrap_content, it should fix your issue.

Upvotes: 0

Related Questions