39

I am using CoordinatorLayout in my activity page. In that there is ListView below the app bar. But its not working when I use ListView instead of NestedScrollView. And if I put ListView inside NestedScrollView, ListView is not expanding

Sandeep Yohans
  • 726
  • 11
  • 27
Bincy Baby
  • 2,409
  • 4
  • 27
  • 53

10 Answers10

70

you can fix it when you add addtribute android:fillViewport="true" in android.support.v4.widget.NestedScrollView :) . This my code.

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:fillViewport="true"
    >
    <ListView
        android:id="@+id/list_myContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        >
    </ListView>

</android.support.v4.widget.NestedScrollView>
ketan
  • 17,717
  • 41
  • 50
  • 83
Chung Nguyen
  • 881
  • 1
  • 7
  • 8
46

on Lollipop onwards you can use

setNestedScrollingEnabled(true);

on your ListView/GridView/ScrollableView. From the documentation

Enable or disable nested scrolling for this view

if you need backwards compatibility with older version of the OS you'll have to use the RecyclerView. You can read more here

Edit. ViewCompat has the static method setNestedScrollingEnabled(View, boolean). Eg.

ViewCompat.setNestedScrollingEnabled(listView, true)

thanks to @Dogcat for pointing it out

Blackbelt
  • 148,780
  • 26
  • 271
  • 285
  • 2
    This answer should definitely be upvoted and even set as the correct answer now. I ended up changing my implementation to a `RecyclerView` because I only read the answer that was accepted as the answer. Yes of course I could've read all the answers, but the first one worked for me - if was just a hassle to change the implementation ;-) – Darwind Nov 08 '16 at 19:28
  • 1
    You just saved me! And yes this should be the accepted answer – liltof Jul 21 '17 at 14:22
  • No, it's not working. Docs say "If this view does not implement nested scrolling this will have no effect." – kreker Sep 04 '17 at 13:57
  • 2
    The ViewCompat does nothing before Android Lolipop: https://stackoverflow.com/questions/32811121/listview-nested-scrolling-on-api21 – Hrk Oct 17 '17 at 07:37
34

For the CoordinatorLayout to work properly you need the scrolling child to implement NestedScrollingChild. Such classes are NestedScrollView and RecyclerView.

To say it short - just use a RecyclerView for your scrolling content and it'll work correctly :)

P.S. As a side note, I don't see a reason why you'd use a ListView anymore. I know it's a habit and it's easier to setup (because you've done it many times), but using a RecyclerView is the recommended way anyways.

Vesko
  • 3,662
  • 2
  • 20
  • 29
13

this is what worked for me.

set android:fillViewport="true" on the NestedScrollView

add One Layout Element as Child to NestedScrollView. In my case LinearLayout and then set android:nestedScrollingEnabled="true" on ListView Make ListView a child of LinearLayout

Good to go

Louis Nuhoho
  • 131
  • 1
  • 4
  • 1
    I was about to say the same thing until I read this. This solution is good for API 21+ – zuko May 14 '18 at 04:01
  • Only solution that's worked for me. And Using the LinearLayout helps in composing a composite ui nested inside the outer NestedScrollView. Thanks! – nemesisfixx Dec 10 '19 at 12:28
10

Just put android:fillViewport="true" inside you NestedScrollView Tag

Shivam
  • 482
  • 4
  • 17
  • Please explain how your answer solves the problem, it will help everyone understand your solution with more clarity and for future reference. – Aziz Apr 03 '16 at 12:13
  • @Aziz actually I was also facing same problem and it worked. on setting "fillViewport" true it stretch the content's height to the viewport's boundaries, – Shivam Apr 04 '16 at 17:37
9

You listview will scroll. Hope help.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    <android.support.v4.widget.NestedScrollView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fillViewport="true"
         app:layout_behavior="@string/appbar_scrolling_view_behavior">

         <ListView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:nestedScrollingEnabled="true">
         </ListView>
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>
Tuan Nguyen
  • 2,151
  • 15
  • 25
3

Below code worked for me:

ViewCompat.setNestedScrollingEnabled(listView, true);

Your ListView should be inside NestedScrollView

Sandeep Yohans
  • 726
  • 11
  • 27
Swati Singh
  • 200
  • 9
1

Replace your ListView with RecyclerView if possible else create your customListView and set onMeasure of ListView to this:

 public NonScrollListView(Context context) {
        super(context);
    }

    public NonScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }

This ListView won't be able to scroll anymore and can be used inside NestedScrollView.

Dipali Shah
  • 3,355
  • 28
  • 46
0

You can't scroll listview inside a nestedscrollview.Use Recyclerview with nestedscrollview

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:orientation="horizontal">

        <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/profile_image"
            android:layout_width="76dp"
            android:layout_height="76dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="24dp"
            android:layout_marginStart="24dp"
            android:src="@drawable/profile"
            app:border_color="#FF000000" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/profile_image"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="IRFAN QURESHI"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="irfan123@gmail.com" />
        </LinearLayout>

        <ImageView
            android:layout_marginLeft="50dp"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_delete_black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"

        android:background="@color/colorPrimary"
        android:gravity="center_horizontal"
        android:padding="30dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/login_email_bg_round_rect_shape"
            android:gravity="center_horizontal"
            android:padding="10dp"
            android:text="POST A QUERY" />
    </LinearLayout>

        <!--<ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>-->

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />


    <RelativeLayout
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:padding="8dp"
            android:gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SIGN OUT" />
        <ImageView
            android:paddingTop="5dp"
            android:layout_marginRight="40dp"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_delete_black" />
    </RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

</android.support.v4.widget.NestedScrollView>
Irfan Qureshi
  • 369
  • 5
  • 7
0

Just add android:nestedScrollingEnabled="true" tag inside your NestedScrollView.

<android.support.v4.widget.NestedScrollView
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:scrollbars="none"
  android:nestedScrollingEnabled="true">
   <ListView
      android:id="@+id/list_myContent"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scrollbars="vertical">
  </ListView>

Aj121
  • 2,361
  • 1
  • 9
  • 8