Forset1
Forset1

Reputation: 117

Viewstub not displacing adjacent views once inflated. Solution?

I have a ViewStub inside a DrawerLayout, and I plan to inflate and remove this stub dynamically according to the needs of the user.

The drawer consists of a RelativeLayout containing: 1. A TextView 2. The mentioned ViewStub 3. A ListView

When I inflate the ViewStub, the resulting view does not displace the ListView. Instead, it's inflated underneath the ListView, in such a way that the user can't see it. I would like for the stub to push the list down when it is inflated.

How can I solve this?

Here is the XML code:

<RelativeLayout

        android:id="@+id/drawer"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#dedede"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        >

        <TextView
            android:id="@+id/drawer_text"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/holo_red_light"/>

        <ViewStub
            android:id="@+id/info_stub"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout="@layout/info_entry_stub"
            android:layout_below="@+id/drawer_text"/>

        <ListView
            android:id="@+id/drawernavlist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_orange_dark"
            android:layout_below="@+id/info_stub"
            android:layout_gravity="start">
        </ListView>

    </RelativeLayout>

The layout the stub points to:

<?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:id="@+id/info_entry_stub">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:text="HELLO"/>


</RelativeLayout>

SOLUTION FOUND: I solved it by adding inside the ViewStub the attribute:

android:inflatedId="@id/info_stub"

Upvotes: 2

Views: 562

Answers (1)

Sadegh
Sadegh

Reputation: 2669

Too late! but just for those who might ran into the same problem.

Once you inflate the ViewStub it will get removed from the view tree hierarchy, then its id won't exits. Therefore, there is no view in the layout with "@+id/info_stub" id, and your ListView wants to be below that (which does not exits)

You can either use a LinearLayout, or use android:inflatedId="@+id/info_layout" in your ViewStub and add android:layout_below="@+id/info_layout" to your ListView

Upvotes: 1

Related Questions