Search code examples
androidandroid-recyclerviewgrid-layout

The last item can't show when RecyclerView in GridLayout


I found that my last item in RecyclerView can't show when RecyclerView inside GridLayot.

I try to remove GridLayot and just use RecyclerView, and it can show all items, but I can't use these solution because I need to show my AppBarLayout too. So I need to use GridLayot to make AppBarLayout and RecyclerView be a group, and SwipeRefreshLayout can use.

How can I fix that, thank you!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite">


    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/activity_cost_diamond_history_swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorComtGray">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnCount="1"
            android:rowCount="2">

            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                android:layout_row="0">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <ImageView
                        android:id="@+id/activity_cost_diamond_history_user_cost_diamond_icon"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_marginRight="5dp"
                        android:src="@drawable/ic_point" />

                    <TextView
                        android:id="@+id/activity_cost_diamond_history_user_cost_diamond_text"
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                        android:gravity="center"
                        android:textColor="@color/colorBlack"
                        android:textSize="17sp" />

                </LinearLayout>

            </com.google.android.material.appbar.AppBarLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/activity_cost_diamond_history_recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical" />

        </GridLayout>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</RelativeLayout>

Solution

  • Make recyclerview row 1

               <androidx.recyclerview.widget.RecyclerView
                 android:layout_row="1"
                android:id="@+id/activity_cost_diamond_history_recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical" />
    

    or Use your layout like this

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">
    
    
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/activity_cost_diamond_history_swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <GridLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:columnCount="1"
                android:rowCount="1">
    
                <com.google.android.material.appbar.AppBarLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginTop="15dp"
                    android:layout_marginBottom="15dp"
                    android:layout_row="0">
    
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:orientation="horizontal">
    
                        <ImageView
                            android:id="@+id/activity_cost_diamond_history_user_cost_diamond_icon"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_marginRight="5dp"
                            android:src="@drawable/ic_back" />
    
                        <TextView
                            android:id="@+id/activity_cost_diamond_history_user_cost_diamond_text"
                            android:layout_width="wrap_content"
                            android:layout_height="50dp"
                            android:gravity="center"
                            android:textColor="@color/background"
                            android:textSize="17sp" />
    
                    </LinearLayout>
    
                </com.google.android.material.appbar.AppBarLayout>
    
    
    
            </GridLayout>
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/activity_cost_diamond_history_recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical" />
        </LinearLayout>
    
    
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>