滚动型与一个ListView不滚动 - 机器人 [英] ScrollView with a ListView doesn't scroll - android

查看:159
本文介绍了滚动型与一个ListView不滚动 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题有以下布局,布局不会滚动。我的要求是,我有显示2列出了有时可大了。所以我把内外滚动型的列表视图,这样我可以滚动至底部,看看这两个列表视图。 我不想在ListView滚动。我只希望我的滚动型的行为,因为它应该。任何建议?

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<滚动型
        机器人:ID =@ + ID / scrollViewContactDetails
        机器人:layout_width =match_parent
        机器人:layout_height =FILL_PARENT机器人:fillViewport =真正的>


        <的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:方向=垂直>


            <的TextView
                机器人:ID =@ + ID /名称
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginLeft =10dp
                机器人:layout_marginTop =5DP
                机器人:textAppearance =机器人:ATTR / textAppearanceLarge
                机器人:TEXTSIZE =22dp/>

            <的LinearLayout
                机器人:ID =@ + ID / numbersLayout
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginLeft =20dp
                机器人:layout_marginRight =20dp
                机器人:方向=垂直
                机器人:weightSum =1>

                <的LinearLayout
                    机器人:ID =@ + ID / numbersLayoutList
                    机器人:layout_width =match_parent
                    机器人:layout_height =FILL_PARENT
                    机器人:重力=中心
                    机器人:方向=垂直
                    机器人:weightSum =1>

                    <的ListView
                        机器人:ID =@ + ID / numbersList
                        机器人:layout_width =match_parent
                        机器人:layout_height =FILL_PARENT
                        机器人:layout_gravity =中心
                        机器人:dividerHeight =2DP>
                    < / ListView控件>
                < / LinearLayout中>
            < / LinearLayout中>

            <的LinearLayout
                机器人:ID =@ + ID / emailLayout
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginLeft =20dp
                机器人:layout_marginRight =20dp
                机器人:layout_marginTop =10dp
                机器人:方向=横向
                机器人:weightSum =1>

                <的LinearLayout
                    机器人:layout_width =120dp
                    机器人:layout_height =match_parent
                    机器人:layout_gravity =右|中心
                    机器人:重力=顶|右
                    机器人:方向=垂直>

                    <的TextView
                        机器人:ID =@ + ID / EMAILID
                        机器人:layout_width =120dp
                        机器人:layout_height =WRAP_CONTENT
                        机器人:layout_marginTop =10dp
                        机器人:重力=右|中心
                        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
                        机器人:文字颜色=#8d8d8d
                        机器人:TEXTSIZE =18dp/>
                < / LinearLayout中>

                <的LinearLayout
                    机器人:ID =@ + ID / emailLayoutList
                    机器人:layout_width =match_parent
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_marginLeft =20dp
                    机器人:重力=中心
                    机器人:方向=垂直
                    机器人:weightSum =1>

                    <的ListView
                        机器人:ID =@ + ID /的emailList
                        机器人:layout_width =match_parent
                        机器人:layout_height =FILL_PARENT
                        机器人:layout_gravity =中心
                        机器人:layout_weight =1
                        机器人:dividerHeight =2DP>
                    < / ListView控件>
                < / LinearLayout中>
            < / LinearLayout中>
        < / LinearLayout中>
    < /滚动型>
< / LinearLayout中>
 

解决方案

我花了好几天,试图找出如何实现这一点,并不能找到任何解决办法 你不应该把一个ListView内滚动型 是恒言我到处搜索。我不想使用的LinearLayout或ViewGroup中,因为我已经创建了使用的ListView整个用户界面,它看起来真棒,我和其他人。它运作良好,除了在网页没有滚动。

最近,我偶然发现了一个问题,在这里,并认为给这个答案一试。它完美的作品!谁说你不应该把一个ListView内的滚动型可能需要相信它的可以的是,如果你的做想的做到这一点。

下面是解决方案:

 公共类工具{
    公共静态无效setListViewHeightBasedOnChildren(ListView控件的ListView){
        ListAdapter listAdapter = listView.getAdapter();
        如果(listAdapter == NULL){
            // pre-条件
            返回;
        }

        INT totalHeight = 0;
        的for(int i = 0; I< listAdapter.getCount();我++){
            查看的listItem = listAdapter.getView(I,空,ListView控件);
            listItem.measure(0,0);
            totalHeight + = listItem.getMeasuredHeight();
        }

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

只需拨打 Utility.setListViewHeightBasedOnChildren(yourListView)已分配的适配器连接到列表视图后,你就大功告成了!

非常感谢 DougW 的想出答案。以下是原文链接我怎样才能把一个ListView成滚动型没有它崩溃?

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. 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. I don't want the ListView to scroll. I only want my ScrollView to behave as it should. 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. 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. 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! Whoever said that you should not put a ListView inside a ScrollView might need to believe that it can be done if you want to do it.

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!

A big thanks to DougW for coming up with the answer. Here is the original link How can I put a ListView into a ScrollView without it collapsing?

这篇关于滚动型与一个ListView不滚动 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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