【问题标题】:recyclerView takes lots of space from top after use SpannedgridLayoutManager使用 SpannedgridLayoutManager 后,recyclerView 从顶部占用大量空间
【发布时间】:2019-12-12 13:42:34
【问题描述】:

我想在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 类中的一些更新或修复错误,但我知道如何解决它

=======编辑======= ~ 在 RecyelerView 之前通过 Give Linearlayout 解决 ~ 不要在你的项目 RecycleView 的 Rootlayout 中提供 Padding 或 Margin

【问题讨论】:

    标签: android android-layout android-recyclerview


    【解决方案1】:

    尝试将您的 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>
    
    ...
    

    【讨论】:

      【解决方案2】:

      因为你的身高是 match_parent 替换为android:layout_height="wrap_content"

      【讨论】:

      • 我试图将我的回收视图和 SwipeRefreshlayoutheight 更改为 wrap_content,但保持间距:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2023-04-05
      • 2014-12-13
      • 2019-03-28
      • 2015-03-21
      相关资源
      最近更新 更多