Nested layout_rowWeight

Nathan Sokalski 4,111 Reputation points
2021-10-28T20:26:47.497+00:00

I have a GridLayout that contains android:layout_height="0dp" and android:layout_rowWeight="1". This works great, except for when I use a row weight inside of it. The GridLayout contains a RecyclerView that also uses android:layout_height="0dp" and android:layout_rowWeight="1". From what I can tell, this causes the RecyclerView to be the same height as it's parent (in other words, it is calculating the height based on the parent of it's parent, rather than it's direct parent). This is causing not all elements to fit. If I set the layout_height of either the immediate parent GridLayout or the parent's parent to a fixed value, everything works fine (except for the fact that I would need to manually calculate that value). Is there a way to use nested row weights, or do I need to manual calculate the heights?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,372 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,966 Reputation points
    2021-10-29T06:24:56.163+00:00

    Hi, njsokalski. I created a basic demo to test the function, the recyclerView is displayed as expected. In the child GridLayout, I placed a textView and the recyclerView.

    Here is the sample code, you could refer to it.

    <?xml version="1.0" encoding="utf-8"?>
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:columnCount="1"
        android:rowCount="3"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:text="the first row"
            android:layout_row="0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        <GridLayout
            android:background="#ff0"
            android:layout_row="1"
            android:columnCount="1"
            android:layout_rowWeight="1"
            android:rowCount="2"
            android:layout_width="match_parent"
            android:layout_height="0dp">
            <TextView
                android:text="testing"
                android:layout_row="0"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <androidx.recyclerview.widget.RecyclerView
                android:background="#ff00"
                android:id="@+id/recyclerView1"
                android:layout_row="1"
                android:layout_rowWeight="1"
                android:layout_width="match_parent"
                android:layout_height="0dp"/>
        </GridLayout>
    
        <TextView
            android:text="the last row"
            android:layout_row="2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </GridLayout>
    

    If the code doesn't work for your project or there are some differences with your project, please post the related code.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.