简体   繁体   中英

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 ( 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 ?

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'

在您的listview上使用ViewCompat.setNestedScrollingEnabled() ,您的问题应得到解决。

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.

  <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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM