Search code examples
androidandroid-layoutandroid-fragmentsandroid-listviewandroid-nested-fragment

Sizing a ListView when it's a nested Fragment


I want a ListView to fill the space available to it while still leaving room for a small footer view at the bottom of the screen. I'm trying to use a RelativeLayout to accomplish this and attempted to use the solution discussed at Limit number of rows of listview . The problem I'm running into is I'm using nested Fragments, so my ListView is actually a FrameLayout in my xml then I load a ListFragment into that frame dynamically. Given the nested fragment stipulation, how can I get my FrameLayout to "stackFromBottom" as I would with a ListView? I just need to stop the list from pushing the other View off the bottom of the screen. Thanks for your time all.


Solution

  • Here is the solution I came up with:

    <TextView
        android:id="@+id/advertisement"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:text="Ads will appear here"
        android:gravity="center"
        android:layout_alignParentBottom="true"/>
    
    <FrameLayout
        android:id="@+id/news_frag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/carousel_menu"
        android:layout_above="@id/advertisement"/>
    

    The trick was to set both layout_above AND layout_below for the FrameLayout, I had only been setting one and that was apparently allowing the layout to push it off of the screen. Also worth noting is they had to be declared in reverse order of how they actually appear on the page, so that the FrameLayout could properly reference the other View.