使用SpannedgridLayoutManager后,recyclerView从顶部占用大量空间 [英] recyclerView takes lots of space from top after use SpannedgridLayoutManager

查看:116
本文介绍了使用SpannedgridLayoutManager后,recyclerView从顶部占用大量空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在recycleview中的SpannedGridLayoutManager中显示列表数据,但是在添加了辅助类SpannedGridLayoutmanager之后,在我的recycleview中需要在顶部留很多空间

i want to show list data in SpannedGridLayoutManager in recycleview, but after add helper class SpannedGridLayoutmanager, in my recycleview take a lot of space in top

iam必须尝试使用​​其他SpannedGridlayout管理器更改SpannedGridLayoutManager类,但顶部空间无法解决

iam has to try change SpannedGridLayoutManager class with other SpannedGridlayout manager, but the space on top is not solved

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

and this is my xml for recycleview

<?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的商品布局

and this is my item layout for 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类中缺少一些更新或修复错误,但是我知道如何解决

e expect i missing some update or fixing bug in class SpannedGridLayoutManager, but i dot know how to solved it

=======编辑======= 〜在RecyelerView之前由Give Linearlayout解决 〜不要在RecycleView项目上给Rootlayout填充或边距

=======EDIT======= ~ Solved by Give Linearlayout before RecyelerView ~ Dont Give Padding or Margin in Rootlayout at yout item RecycleView

推荐答案

尝试将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>

...

这篇关于使用SpannedgridLayoutManager后,recyclerView从顶部占用大量空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆