JayHawk
JayHawk

Reputation: 295

How to scroll a ListView without using NestedScrollView?

This question has probably been asked at least a dozen times in the past. Seen previous responses but still can't seem to get it to work

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/colors_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

I do have a workable solution which I do not wish to use since I am told there is no need to use NestedScrollView since ListView is scrollable by itself. Yet, I can't seem to scroll my ListView if I use it by itself. Why?

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/layout_background_color">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:orientation="vertical">

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

</LinearLayout>

</android.support.v4.widget.NestedScrollView>

Upvotes: 0

Views: 59

Answers (2)

utsmannn
utsmannn

Reputation: 51

Your nested scrollview always push listview to scroll down. Wraping listview or recyclerview with scrollview is bad way, your ui can be heavy. Try another method like recyclerview multiple view holder for better building scrolling ui with list

Upvotes: 0

duongdt3
duongdt3

Reputation: 1678

I guess the big problem here that is you set ListView with android:layout_height="wrap_content", it can not scroll because of its height always increases by its children.

Take a try with android:layout_height="match_parent". (Magic happened!)

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:orientation="vertical">

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

</LinearLayout>

Upvotes: 2

Related Questions