Search code examples
android-linearlayoutexpandablelistview

LinearLayout works differently in ExpandableListView


I'm trying to implement a book contents selector using ExpandableListView, but I'm finding that LinearLayout seems to work differently when inside the ELV, than it does in an activity.

To illustrate what I am trying to do, here is where I am at so far:

Screen Capture

enter image description here

Notice when the text wraps to the next line, the TextView fills the available space and aligns the vertical line (and button in the groupView) to the right (section 1.1 and 3), but if the text does not wrap, the TextView only takes up as much space as it needs.

Here is the child view layout, the group view is similar but with a button.

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

        <View android:layout_width="3dp" android:layout_height="match_parent" android:background="#000000" />
        <View android:layout_width="10dp" android:layout_height="match_parent" />

        <TextView
            android:id="@+id/sectionNumber"
            android:layout_width="65sp"
            android:layout_height="wrap_content"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/sectionTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="18sp" />

        <View android:layout_width="6dp" android:layout_height="match_parent" />
        <View android:layout_width="3dp" android:layout_height="match_parent" android:background="#000000" />

    </LinearLayout>

When I tried this outside of an ExpandableListView, it works as intended. What am I missing?


Solution

  • The problem was elsewhere in the fragment xml layout:

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/expandableListView" />
    

    layout_width was set to "wrap_content" and should have been "match_parent"