开发者

Why is my listview not scrolling?

开发者 https://www.devze.com 2023-03-30 21:49 出处:网络
<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" android:layout_width=\"fill_parent\"
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    a开发者_如何学Cndroid: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>


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>


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>


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).


I think listview can scroll by default, use three fingers to swipe the virtual device. I do nothing with my listview and it can scroll. I drag ListView under design mode.

<ListView
        android:id="@+id/lv_retrieve_firebase"
        android:layout_width="match_parent"
        android:layout_height="287dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        app:layout_constraintBottom_toTopOf="@+id/tw_login_email"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_submit_to_firebase" />


The problem I was facing was I added a "ListView" in "ConstraintLayout". For that, my ListView was neither showing properly nor scrolling properly.

I found the solution by wrapping a LinearLayout with ListView.

Previously I was using:

Why is my listview not scrolling?

Then The Solution was like:

Why is my listview not scrolling?


For me :

nestedScrollingEnabled = true


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消