简体   繁体   English

RelativeLayout内的Android RecyclerView无法正确滚动

[英]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. 我正在制作一个具有活动的Android应用程序,用户在其中选择要生成的随机数,然后显示数字。 This is all inside a RelativelLayout that has a ScrollView inside it. 所有这些都在其中具有ScrollViewRelativelLayout内部。 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 . 在滚动视图中,这是一个静态CardView ,用户可以在其中选择所需的编号,然后单击一个按钮,该按钮将生成并在RecyclerView显示该按钮(动态创建用户选择的相同CardView数量)。 CardView I have it all working right except for the scrolling and spacing between the CardView added dynamically. 除了动态添加的CardView之间的滚动和间距之外,我都正常工作。 The CardViews in the Recylerview are somehow being spaced really big. CardViews中的Recylerview间距很大。

Below is a screenshot of the issue: 以下是问题的屏幕截图:

以下是问题的屏幕截图:

My CardView Item Layout Code: 我的CardView项目布局代码:

<?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: 我的MainActivity代码:

<?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>

In the line 6 of your item layout replace 在项目布局的第6行中,替换

android:layout_height="match_parent"

with

android:layout_height="wrap_content"

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. 我认为发生的事情是,由于CardView的RelativeLayout的高度为match_parent ,因此它使cardview的高度与整体布局的高度匹配。 If you change this to wrap_content , it should fix your issue. 如果将其更改为wrap_content ,它将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM