无法在其父NestedScrollView中滚动ListView-Android [英] Can't scroll ListView in its parent NestedScrollView - Android

查看:90
本文介绍了无法在其父NestedScrollView中滚动ListView-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NestedScrollView中使用了ListView,但是在ListView中看不到超过1个项目.我可以滚动到TextView( textContact )的末尾,但不能滚动到ListView( listContact ).

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

这是.xml文件的代码:

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 ?

推荐答案

您需要在 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">

然后使用帮助程序类扩展 ListView 的宽度&修复滚动功能,

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));
    }
}

要在设置 ListView 适配器时使用帮助程序类,

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:确保已为项目添加了支持设计库.

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

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

这篇关于无法在其父NestedScrollView中滚动ListView-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆