0

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

Here's the code for .xml File :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    ... >
    <android.support.design.widget.CoordinatorLayout
        ... >    
        <LinearLayout
            ...
            android:orientation="vertical">

            <android.support.v7.widget.Toolbar
                ... />
            <android.support.v4.widget.NestedScrollView
                ... >
                <LinearLayout
                    ...
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textContact"
                        ... />
                    <LinearLayout
                        ...
                        android:orientation="vertical">
                        <ListView
                            android:id="@+id/listContact"
                            ... />
                    </LinearLayout>
                </LinearLayout>

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

        </LinearLayout>

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        ... />

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

Is there any design issue, any compatibility issue of the Widgets/Controls used here, or anything else ?

halfer
  • 18,701
  • 13
  • 79
  • 158
Somendra Meena
  • 129
  • 3
  • 12

4 Answers4

5

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;

public class Helper {
    public static void getListViewSize(ListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter == null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {
            View listItem = myListAdapter.getView(size, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
      //setting listview item in adapter
        ViewGroup.LayoutParams params = 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(new ArrayAdapter<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'
Prokash Sarkar
  • 10,714
  • 1
  • 30
  • 47
3

Use ViewCompat.setNestedScrollingEnabled() on your listview and your problem should be solved.

Ashwin
  • 118
  • 1
  • 8
0

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.

Piotr Wittchen
  • 3,356
  • 4
  • 22
  • 31
  • Yes I want company's info and then custom ListView for social buttons, instead of adding buttons for each social network. So it would be better if possible. – Somendra Meena Aug 24 '16 at 21:00
0
  <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

Srishti Roy
  • 536
  • 5
  • 16
  • **Firstly,** I am not using RecyclerView, and **Second**, by adding that property, only ListView is now expanded. But if it gets out of Screen at Activity Startup, then I can't see full ListView by scrolling, again only first item is visible. – Somendra Meena Aug 23 '16 at 15:47
  • http://stackoverflow.com/questions/27070079/expand-collapse-lollipop-toolbar-animation-telegram-app – Srishti Roy Aug 24 '16 at 04:10