首页 > 解决方案 > Android Grid View 一些元素重叠

问题描述

我有这个网格视图,它动态填充了从数据库中获取的数据。数据大小不同,因此每个元素都有不同的高度虽然大多数元素会重叠,但有些元素会重叠。奇怪的部分是在滚动过去该项目后它停止重叠并正确放置

这是它的样子

具有网格视图的片段 这是具有网格视图的片段

<?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"
    tools:context="com.app.allindiainfo.ui.priceList.ListingsFragment">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/my_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/secondary_blue"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/listings_grid">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/filter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/orange"
                android:layout_margin="5dp"
                android:layout_alignParentEnd="true"
                android:text="Filter" />
        <SearchView
            android:id="@+id/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/white"
            android:lineSpacingExtra="0dp"
            android:lineSpacingMultiplier="0"
            android:background="@drawable/searchbar_layout"
            android:queryBackground="@color/white"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="5dp"
            android:iconifiedByDefault="false"
            android:clickable="true"
            android:layout_toLeftOf="@+id/filter"
            android:queryHint="Search Price List"
            />

            <TextView
                android:id="@+id/title"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Title"
                android:textSize="18sp"
                android:textStyle="bold"
                android:textAlignment="center"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:background="@color/secondary_blue"
                android:padding="10dp"
                android:layout_below="@+id/search"/>
    </RelativeLayout>
    </androidx.appcompat.widget.Toolbar>
    
    <GridView
        android:id="@+id/listings_grid"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:numColumns="2"
        android:gravity="end"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/my_app_bar" />


</androidx.constraintlayout.widget.ConstraintLayout>

每个元素的布局 这是从数据库接收数据并显示它的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <androidx.cardview.widget.CardView
        android:id="@+id/pricecard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:padding="20dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="15dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


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

            <ImageView
                android:id="@+id/pricecard_image"
                android:layout_width="match_parent"
                android:layout_height="170dp"
                android:layout_gravity="fill_horizontal"
                android:layout_marginBottom="5dp"
                android:cropToPadding="true"
                android:scaleType="fitXY"
                android:src="@drawable/placeholder" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingRight="12dp"
                android:paddingLeft="12dp"
                android:paddingBottom="12dp">

                <TextView
                    android:id="@+id/pricecard_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:text="Ashirvad"
                    android:textColor="@color/black"
                    android:textSize="@dimen/_16sdp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/pricecard_effectivedate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    app:autoSizeTextType="uniform"
                    android:text="Effective From: 05-01-2021"
                    android:textSize="@dimen/_10sdp" />


            </LinearLayout>
        </LinearLayout>

    </androidx.cardview.widget.CardView>
</RelativeLayout>

我希望所有元素都具有相同的宽度相同的高度并不重要,但如果能以某种方式实现将会很棒。

标签: javaandroidfirebaseandroid-studio

解决方案


推荐阅读