Skip to content Skip to sidebar Skip to footer

Can't Scroll Listview In Its Parent Nestedscrollview - Android

I am using ListView within NestedScrollView, but can't see more than 1 item in ListView. I can scroll upto the end of TextView (textContact) but can't scroll within the ListView (l

Solution 1:

You need to enable the following two properties inside the NestedScrollView,

<android.support.v4.widget.NestedScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

Then use a helper class to extend the ListView width & fix scrolling functionality,

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

publicclassHelper {
    publicstaticvoidgetListViewSize(ListView myListView) {
        ListAdaptermyListAdapter= myListView.getAdapter();
        if (myListAdapter == null) {
            //do nothing return nullreturn;
        }
        //set listAdapter in loop for getting final sizeinttotalHeight=0;
        for (intsize=0; size < myListAdapter.getCount(); size++) {
            ViewlistItem= myListAdapter.getView(size, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
      //setting listview item in adapter
        ViewGroup.LayoutParamsparams= myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
        Log.i("height of listItem:", String.valueOf(totalHeight));
    }
}

To use the helper class while setting the ListView adapter,

listView.setAdapter(newArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
              Helper.getListViewSize(listView);

PS: Make sure you have added support design lib for your project.

compile'com.android.support:design:23.4.0'

Solution 2:

Solution 3:

I think, you have bad layout design and you should redesign it. In general, you shouldn't have a scrollable view like ListView inside another scrollable view like NestedScrollView or ScrollView. Do you really need that? Try to change it, flatten your views structure and this problem should disappear.

Solution 4:

  <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:clipToPadding="false" >
       </android.support.v7.widget.RecyclerView>

Are you missing app:layout_behavior

Post a Comment for "Can't Scroll Listview In Its Parent Nestedscrollview - Android"