简体   繁体   English

带有 ListView 的 ScrollView 不滚动 - android

[英]ScrollView with a ListView doesn't scroll - android

My problem with the following layout is that the layout doesn't scroll.我对以下布局的问题是布局不滚动。 My requirement is that I have to display 2 lists which at times can be big.我的要求是我必须显示 2 个有时可能很大的列表。 So i put both the ListViews inside a ScrollView so that i can scroll to the bottom and have a look at both the ListViews.所以我把两个 ListViews 放在一个 ScrollView 中,这样我就可以滚动到底部并查看两个 ListViews。 I don't want the ListView to scroll . I don't want the ListView to scroll I only want my ScrollView to behave as it should.我只希望我的 ScrollView 表现得像它应该的那样。 Any suggestions??有什么建议么??

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
        android:id="@+id/scrollViewContactDetails"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" android:fillViewport="true">


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >


            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="22dp" />

            <LinearLayout
                android:id="@+id/numbersLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:orientation="vertical"
                android:weightSum="1" >

                <LinearLayout
                    android:id="@+id/numbersLayoutList"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:weightSum="1" >

                    <ListView
                        android:id="@+id/numbersList"
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:dividerHeight="2dp" >
                    </ListView>
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/emailLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                android:weightSum="1" >

                <LinearLayout
                    android:layout_width="120dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right|center"
                    android:gravity="top|right"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/emailId"
                        android:layout_width="120dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="right|center"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="#8d8d8d"
                        android:textSize="18dp" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/emailLayoutList"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginLeft="20dp"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:weightSum="1" >

                    <ListView
                        android:id="@+id/emailList"
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:dividerHeight="2dp" >
                    </ListView>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

I spent days trying to figure out how to achieve this and couldn't find any solution.我花了几天时间试图弄清楚如何实现这一目标,但找不到任何解决方案。 You should not put a ListView inside a ScrollView was the common saying everywhere I searched. You should not put a ListView inside a ScrollView是我搜索过的任何地方的普遍说法。 I didn't want to use LinearLayout or ViewGroup because I had already created the whole UI using ListView and it looked awesome to me and everyone else.我不想使用 LinearLayout 或 ViewGroup,因为我已经使用 ListView 创建了整个 UI,它对我和其他人来说都很棒。 It worked well except that the page didn't scroll.除了页面没有滚动之外,它运行良好。

Recently I stumbled upon a question here and thought to give this answer a try.最近我在这里偶然发现了一个问题,并想尝试一下这个答案。 It works flawlessly!它完美无缺!

Here's the solution:这是解决方案:

public class Utility {
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}

Just call Utility.setListViewHeightBasedOnChildren(yourListView) after you have assigned the adapter to your listview and you're done!将适配器分配给列表视图后,只需调用Utility.setListViewHeightBasedOnChildren(yourListView)即可!

A big thanks to DougW for coming up with the answer.非常感谢DougW提出答案。 Here is the original link How can I put a ListView into a ScrollView without it collapsing?这是原始链接如何将 ListView 放入 ScrollView 而不折叠?

You shouldn't put listview in to scrollview, it's not a good idea.您不应该将列表视图放入滚动视图,这不是一个好主意。 If you don't want it to scroll, maybe you shouldn't use listviews.如果您不希望它滚动,也许您不应该使用列表视图。 Have you tried with LinearLayouts?你试过 LinearLayouts 吗?

Instead of putting two listview you can use one listview and you can add both data or inflate both xml file in the adapter of that listview.您可以使用一个列表视图而不是放置两个列表视图,并且可以在该列表视图的适配器中添加两个数据或扩充两个 xml 文件。 This will show both data in one listview and scroll the data with one scroll bar.这将在一个列表视图中显示两个数据并使用一个滚动条滚动数据。

Before calling the below method,在调用下面的方法之前,

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

You must remove the property 'android:fillViewport="true" from scrollview.您必须从滚动视图中删除属性 'android:fillViewport="true"。 If not, scrolling not works.如果没有,滚动不起作用。

Try this way:试试这个方法:

You removed scroll view from your layout because that is not working correctly for list view..您从布局中删除了滚动视图,因为列表视图无法正常工作。

And also you put the height "android:layout_height="fill_parent" for listview so that is occupy full height of parent layout.并且您还为列表视图放置了高度"android:layout_height="fill_parent" ,以便占据父布局的整个高度。

so please fix some height manually for both listview.所以请为两个列表视图手动修复一些高度。

found the solution just add :找到解决方案只需添加:

android:nestedScrollingEnabled="true"

to the List view :3到列表视图:3

            <ListView
                android:id="@+id/ListViewPrzebiegTrasy"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:visibility="visible"
                android:scrollbars="vertical"
                android:nestedScrollingEnabled="true">
            </ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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