androidlistviewandroid-listviewgrid-layout

ListView inside GridLayout


I have a ListView inside a android.support.v7.widget.GridLayout, here is my code:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:columnCount="4"
        app:orientation="horizontal">

        <!-- Sort -->
        <Space
            android:layout_width="@dimen/dialog_content_space"
            app:layout_gravity="fill_vertical"
            app:layout_rowSpan="2" />

        <TextView
            style="@style/TextView"
            android:layout_width="@dimen/none"
            android:layout_height="@dimen/dialog_content_row_height"
            android:text="@string/sort"
            app:layout_columnWeight="1" />

        <Spinner
            android:id="@+id/spinner_sort"
            style="@style/Spinner"
            android:layout_width="@dimen/none"
            android:layout_height="@dimen/dialog_content_row_height"
            app:layout_columnWeight="1" />

        <Space
            android:layout_width="@dimen/dialog_content_space"
            app:layout_gravity="fill_vertical"
            app:layout_rowSpan="2" />

        <TextView
            android:id="@+id/txt_filter"
            style="@style/TextView"
            android:layout_width="@dimen/none"
            android:layout_height="@dimen/dialog_content_row_height"
            android:text="@string/filter"
            app:layout_columnWeight="1" />

        <Spinner
            android:id="@+id/spinner_filter"
            style="@style/Spinner"
            android:layout_width="@dimen/none"
            android:layout_height="@dimen/dialog_content_row_height"
            app:layout_columnWeight="1" />

        <include
            layout="@layout/divider"
            android:layout_width="match_parent"
            android:layout_height="@dimen/divider_height"
            android:layout_marginTop="@dimen/divider_margin"
            app:layout_columnSpan="4" />

        <ListView
            android:id="@+id/layout_courses"
            android:layout_width="@dimen/none"
            android:layout_height="@dimen/none"
            app:layout_rowWeight="1"
            app:layout_gravity="fill"
            app:layout_columnSpan="4"
            android:divider="@null"
            android:scrollbars="none"/>

        <Space
            android:layout_width="@dimen/none"
            android:layout_height="12dp"
            app:layout_gravity="fill"
            app:layout_columnSpan="4"/>
    </android.support.v7.widget.GridLayout>`

What it's shown in the Design tab in AndroidStudio it's exactly what I wanted to achieve, but when I run my app in an Android device the ListView it's completely empty.

I'm guessing it has something to do with the fact that the ListView width and height are both 0dp (that's the value for @dimen/none), but with these values it's the only way I can get my desired result.


Solution

  • Why not just place the ListView below the GridLayout? The layout doesn't really need to be constructed with the ListView being apart of the GridLayout since you are just trying to create a fixed header above the ListView for sorting and filtering :)