BMB
BMB

Reputation: 1638

Android ListView cutting off items wider than the first item

I have a Listview inside of a HorizontalScrollView. The listview holds file paths, and the horizontal scroll view is intended to eliminate the need to wrap the longer paths.

Everything is working exactly as desired with the exception that any items longer than the first item in the listview are being cut of at the length of the first item (whatever the width of the first item, none of the other items will scroll past that width).

Here is the section of the layout that is giving me trouble:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layoutListView"
    android:orientation="vertical"
    android:layout_below="@+id/spFileTypes">

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/lvItems"/>

    </HorizontalScrollView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text=""
        android:id="@+id/tvStatus"/>

</LinearLayout>

Here is a screenshot of the issue:

listview issue screenshot

The image is scrolled all the way to the right. The third item is cutting off ".php". I have tried this with long and short file names, lists of many (upper double digits) and lists with just a few, in each case, it seems the width limit is dependent of the width of the first item.

Whenever the lists changes, it always limits its width to whatever the first element is.

The desired behavior is to have it match the width of the widest item.

I've googled my heart out, dug through the official documentation, and I'm stumped. Any help is greatly appreciated :)

Upvotes: 2

Views: 693

Answers (1)

Lazycoder-007
Lazycoder-007

Reputation: 1205

Instead of wrapping the ListView in HorizontalScrollView, try this:

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="horizontal"/>

Upvotes: 1

Related Questions