Why is my listview not scrolling?

2020-06-03 09:18发布

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#dad4c8"
    android:gravity="center_horizontal">
    <include
        android:id="@+id/title_bar"
        layout="@layout/title_bar" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title_bar"
        android:id="@+id/txt"
        android:hint="Song title" />

    <ListView
        android:cacheColorHint="#00000000"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txt"
        android:id="@+id/list" />

</RelativeLayout>

4条回答
狗以群分
2楼-- · 2020-06-03 09:43

Those who still looking for the answer please visit this link you just need to do this inchild scroll view by adding android:nestedScrollingEnabled="true" to its XML declaration or by explicitly calling setNestedScrollingEnabled(true).

查看更多
3楼-- · 2020-06-03 09:45

You should add the property android:scrollbars with the value vertical on your ListView to add the scrollbar in your listView:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#dad4c8"
    android:gravity="center_horizontal">
    <include
        android:id="@+id/title_bar"
        layout="@layout/title_bar" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title_bar"
        android:id="@+id/txt"
        android:hint="Song title" />

    <ListView
        android:cacheColorHint="#00000000"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txt"
        android:id="@+id/list" 
        android:scrollbars="vertical"/>

</RelativeLayout>
查看更多
放我归山
4楼-- · 2020-06-03 09:58

For the benefit of the community, I'll paste what did not work for me and what worked. This is the only post that helped fix the problem, others were just a lecture. Thanks to @Kurru

I have tried to reduce the crap. The areas to focus on are android:layout_height android:layout_weight.

Did not work

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content />
    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbarSize="3dp"
        android:scrollbarStyle="outsideOverlay"
        android:scrollbars="vertical"
        android:scrollingCache="true"
        android:smoothScrollbar="true" />
</LinearLayout>

Worked

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:scrollbarSize="3dp"
        android:scrollbarStyle="outsideOverlay"
        android:scrollbars="vertical"
        android:scrollingCache="true"
        android:smoothScrollbar="true" />
</LinearLayout>
查看更多
▲ chillily
5楼-- · 2020-06-03 10:03

Use a LinearLayout and set the ListView's Height to 0dip and give it layout_weight="1"

This will make it autofill any remaining space, causing the internal items of the list view to scroll. Currently the listview is very tall and is being clipped by the bounds of the screen.

Edit

Something like this:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#dad4c8"
    android:orientation="vertical"
    android:gravity="center_horizontal">
    <include
        android:id="@+id/title_bar"
        layout="@layout/title_bar" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt"
        android:hint="Song title" />

    <ListView
        android:cacheColorHint="#00000000"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:id="@+id/list" />

</LinearLayout>
查看更多
登录 后发表回答