首页 > 解决方案 > 当我向下滚动时,CardView 布局之间的间距很大

问题描述

我正在制作一种库存搜索应用程序。我有一个问题,我尝试在突然出现的卡片视图之间上下滚动一个巨大的空间。当我进行搜索时,布局会重置为正常,但是当我进行滚动时,问题就出现了。下面是我的 list_layout.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="6dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="1dp"
        app:cardCornerRadius="17dp"
        android:background="#EFEFEF"
        app:cardElevation="1dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:stretchColumns="0,1"
                android:layout_marginTop="20dp">

                <TableRow
                    android:layout_width="match_parent"
                    android:paddingBottom="8dp"
                    android:layout_height="match_parent">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="170dp"
                            android:layout_height="150dp"
                            android:layout_column="0"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/textViewName"
                            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:textColor="#000000"
                            android:textSize="15dp"
                            android:textStyle="bold">

                        </TextView>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="10dp"
                            android:text="Over 700 items in store!"
                            android:textColor="#000000"
                            android:textSize="12dp"
                            android:textStyle="bold">

                        </TextView>

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="#FF0000">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="ON SALE !"
                                android:textColor="#fff"
                                android:textStyle="bold">

                            </TextView>

                        </LinearLayout>

                        <Button
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_marginTop="19dp"
                            android:background="@drawable/checkout_button"
                            android:onClick="groceries_click"
                            android:text="shop now!"
                            android:textAllCaps="false"
                            android:textSize="12dp">
                        </Button>
                    </LinearLayout>
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

视图的图像在我向下滚动之前得到。 || 这是我在滚动后得到的行为

标签: androidxmllistviewsearchandroid-linearlayout

解决方案


它发生是因为你match_parent在你的标签上<LinearLayout/>

变成wrap_content

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" //Change into wrap_content
    android:padding="6dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

推荐阅读