簡體   English   中英

使用SpannedgridLayoutManager后,recyclerView從頂部占用大量空間

[英]recyclerView takes lots of space from top after use SpannedgridLayoutManager

我想在recycleview中的SpannedGridLayoutManager中顯示列表數據,但是在添加了輔助類SpannedGridLayoutmanager之后,在我的recycleview中占據了很多空間

iam必須嘗試使用​​其他SpannedGridlayout管理器更改SpannedGridLayoutManager類,但是頂部空間無法解決

val manager = SpannedGridLayoutManager(object : SpannedGridLayoutManager.GridSpanLookup{
            override fun getSpanInfo(position: Int): SpannedGridLayoutManager.SpanInfo {
                // Conditions for 2x2 items
                return if (position % 12 == 0 || position % 12 == 7) {
                    SpannedGridLayoutManager.SpanInfo(2, 2)
                } else {
                    SpannedGridLayoutManager.SpanInfo(1, 1)
                }
            }

        },3/*column*/,1f/*how big is default item*/)

        binding.feedHome.setHasFixedSize(false)
        binding.feedHome.layoutManager = manager
        binding.feedHome.adapter = adapter

這是我的recycleview XML

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/swipeRefreshLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:id="@+id/feed_home"
                    android:windowSoftInputMode="adjustResize"
                    tools:listitem="@layout/item_feed_home"
                    android:layout_height="match_parent"
                    tools:ignore="MissingConstraints"/>

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>



        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/no_data"
                android:visibility="gone"
                android:text="@string/no_data_mypost"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                android:layout_gravity="center"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

這是我的recycleview項目布局

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <com.google.android.material.card.MaterialCardView
            android:orientation="vertical"
            android:layout_width="match_parent"
            app:cardCornerRadius="8dp"
            app:cardElevation="4dp"
            app:cardMaxElevation="6dp"
            app:strokeWidth="4dp"
            app:cardBackgroundColor="@color/white"
            android:layout_height="match_parent"
            android:background="@color/grey_10"
            android:layout_marginBottom="1.5dp"
            android:layout_marginEnd="1.5dp"
            android:id="@+id/material_global_zona"
            android:layout_marginTop="1.5dp">



        <ImageView
                android:scaleType="fitXY"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/feed_post_image"
                android:src="@drawable/sample_kelinci"
                tools:ignore="MissingConstraints"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>
        <!--<TextView android:layout_width="match_parent" android:text="0"  android:textAlignment="center" android:textSize="70sp" android:textColor="@color/white" android:textAppearance="@style/TextAppearance.AppCompat.Large" android:id="@+id/count" android:layout_height="wrap_content"/>-->


    </com.google.android.material.card.MaterialCardView>

</layout>

我期望我在類SpannedGridLayoutManager中缺少一些更新或修復錯誤,但是我知道如何解決它

recycleview有一些間距

=======編輯=======〜通過在RecyelerView之前提供Linearlayout解決〜在項目RecycleView處不要在Rootlayout中提供填充或邊距

因為您的身高是match_parent,請替換為android:layout_height="wrap_content"

嘗試將您的RecyclerView包裝在LinearLayout

...

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

            <androidx.recyclerview.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:id="@+id/feed_home"
                    android:windowSoftInputMode="adjustResize"
                    tools:listitem="@layout/item_feed_home"
                    android:layout_height="match_parent"
                    tools:ignore="MissingConstraints"/>

        </LinearLayout>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM