我怎么能自动调整大小列表视图,因此它不会滚动 [英] how can i automatically size listview so it doesn't scroll

查看:86
本文介绍了我怎么能自动调整大小列表视图,因此它不会滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有以下布局

 <的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentRight =真

    机器人:layout_marginTop =9px
    机器人:layout_below =@ + ID /递减
    机器人:ID =@ + ID / ll_item
    机器人:方向=垂直
    机器人:paddingRight =的3px
    机器人:以下属性来=的3px
    机器人:paddingBottom会=5像素
    机器人:paddingTop =5像素
    机器人:背景=@可绘制/ rounded_corner_lists>
<! - 
        < ListView的机器人:drawSelectorOnTop =假机器人:ID =@ + ID / LV机器人:layout_height =FILL_PARENT机器人:layout_width =WRAP_CONTENT机器人:分隔=#DDD的Andr​​oid版本:dividerHeight =1像素机器人:背景=@可绘制/白/>
     - >
    < / LinearLayout中>
 

这是我已经注释掉的列表视图,我试图让这个在XML,与高度设置为WRAP_CONTENT,FILL_PARENT,目前我在做这个编程具有以下code

 的LinearLayout ll_item =(的LinearLayout)this.findViewById(R.id.ll_item);
        如果(list.length()大于0)
        {
            ll_item.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,calcListHeight(名单));

            列表视图=新的ListView(本);
            listview.setBackgroundResource(R.drawable.white);
            listview.setDivider(新ColorDrawable(this.getResources()的getColor(R.drawable.dividercolor)));
            listview.setDividerHeight(1);
            listview.setCacheColorHint(0);

            mAdapter =新JSONAdapter(列表中,这一点);
            listview.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
            ll_item.addView(列表视图,LP);
        }
 

这是结果

这样你就可以在这个图片看到,因为我包含列表视图中的LinearLayout获得圆角的外观,它不只是自动延伸到包含整个列表视图,有没有什么办法有两个元素只是包装内容垂直所以没有我编程设定的高度没有滚动? ? ?

我想一件事,我应该提到的是,我有所有这些布局在滚动视图,因为我想这个列表视图是整个布局的一个微小的款,所以这会是这样

-scrollview
  -textview
  -textview

  -linearlayout
    -listview

   - 按钮

下面是什么,我有一个简单的布局

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout机器人:方向=垂直机器人:layout_height =FILL_PARENT机器人:layout_width =FILL_PARENT
              机器人:背景=@可绘制/ BG的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:ID =@ + ID /标题栏>

    <滚动型机器人:ID =@ + ID / SV机器人:layout_width =FILL_PARENT机器人:背景=@可绘制/ BG
                机器人:layout_height =FILL_PARENT的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

        < RelativeLayout的机器人:ID =@ + ID / widget28
                        机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT机器人:填充=4dip
                >


            <的LinearLayout机器人:方向=垂直的风格=@风格/ rounded_corner_full_width_button
                          机器人:ID =@ + ID / editfields>

                < ListView的机器人:drawSelectorOnTop =假机器人:ID =@ + ID / LV机器人:layout_height =FILL_PARENT
                          机器人:layout_width =WRAP_CONTENT机器人:分隔=#DDD的Andr​​oid版本:dividerHeight =1像素
                          机器人:背景=@可绘制/白/>

            < / LinearLayout中>
        < / RelativeLayout的>
    < /滚动型>
< / LinearLayout中>
 

解决方案

列表视图不进去ScrollViews。

ListView控件是有效地显示一个有限的窗口,进入无限的内容。如果你为Disable滚动上一个ListView把它滚动型中你失去所有实际理由摆在首位使用一个ListView。

如果你想用一个ListView显示大量的内容或无限的内容,但也有内容的上方和下方,与它滚动,使用添加页眉或页脚的意见到ListView <一href="http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29"><$c$c>addHeaderView或<一href="http://developer.android.com/reference/android/widget/ListView.html#addFooterView%28android.view.View%29"><$c$c>addFooterView.如果列表的内容将是你的整体布局为你描述的一小部分,这可能不是最适合你的方法。

如果你有一个小,有界设定的内容,以present,继续使用滚动型和编程方式生成子视图为贵列表项在适当情况下。

在框架中使用混合膨胀的XML内容与程序生成内容的一种常用的模式是添加一个占位符视图在布局XML,通常是的LinearLayout 的FrameLayout 。使用 findViewById 来定位它在运行时产生的子视图添加到它。

您甚至可以仍然使用 ListAdapter 使用这种方法,如果你有一个已经编写,只需拨打 content.addView(adapter.getView(位置,空,内容))在一个循环中的所有适配器的位置(其中内容是位于与占位符视图 findViewById )。需要注意的是,如果你知道你有在适配器少数列表中的项目,这是唯一可行的!

currently i have the following layout

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 

    android:layout_marginTop="9px" 
    android:layout_below="@+id/desc" 
    android:id="@+id/ll_item"  
    android:orientation="vertical" 
    android:paddingRight="3px" 
    android:paddingLeft="3px" 
    android:paddingBottom="5px" 
    android:paddingTop="5px" 
    android:background="@drawable/rounded_corner_lists" >
<!--
        <ListView android:drawSelectorOnTop="false" android:id="@+id/lv"    android:layout_height="fill_parent" android:layout_width="wrap_content" android:divider="#ddd" android:dividerHeight="1px" android:background="@drawable/white"    />
    -->
    </LinearLayout>

the listview that i have commented out, i have tried to make this in the xml, with the height set to wrap_content, fill_parent, currently i am doing this programatically with the following code

LinearLayout ll_item = (LinearLayout) this.findViewById(R.id.ll_item);
        if(list.length() > 0)
        {
            ll_item.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,calcListHeight(list));

            listview = new ListView(this);
            listview.setBackgroundResource(R.drawable.white);
            listview.setDivider( new ColorDrawable(this.getResources().getColor(R.drawable.dividercolor)) );
            listview.setDividerHeight(1);
            listview.setCacheColorHint(0);

            mAdapter  = new JSONAdapter( list, this );
            listview.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
            ll_item.addView(listview, lp);
        }

this is the result

so you can see in this image, that since i'm containing the listview in a linearlayout to get the rounded corner look, it doesn't just automatically stretch to contain the entire listview, is there any way to have the two elements just wrap the content vertically so there is no scrolling without me programatically setting the height ? ? ?

i guess one other thing i should mention is that i have all this layout in a scrollview, because i want this listview to be a tiny subsection of the entire layout, so it would be something like

-scrollview
-textview
-textview

-linearlayout
-listview

- button

here is a simpler layout of what i have

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent"
              android:background="@drawable/bg" xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/titlebar">

    <ScrollView android:id="@+id/sv" android:layout_width="fill_parent" android:background="@drawable/bg"
                android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

        <RelativeLayout android:id="@+id/widget28"
                        android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="4dip"
                >


            <LinearLayout android:orientation="vertical" style="@style/rounded_corner_full_width_button"
                          android:id="@+id/editfields">

                <ListView android:drawSelectorOnTop="false" android:id="@+id/lv" android:layout_height="fill_parent"
                          android:layout_width="wrap_content" android:divider="#ddd" android:dividerHeight="1px"
                          android:background="@drawable/white"/>

            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</LinearLayout>

解决方案

ListViews do not go in ScrollViews.

ListView is for displaying a limited window into unbounded content efficiently. If you were to "disable scrolling" on a ListView to put it within a ScrollView you lose all practical reason for using a ListView in the first place.

If you want to use a ListView to show lots of content or unbounded content but also have content above and below that scrolls with it, add header or footer views to the ListView using addHeaderView or addFooterView. If the list content is going to be a small portion of your overall layout as you describe, this probably isn't the best approach for you.

If you have a small, bounded set of content to present, go ahead and use a ScrollView and programmatically generate child views for your "list items" where appropriate.

A common pattern used in the framework to mix inflated XML content with programmatically generated content is to add a placeholder view in the layout XML, usually a LinearLayout or FrameLayout. Use findViewById to locate it at runtime and add generated child views to it.

You could even still use a ListAdapter with this approach if you have one written already, just call content.addView(adapter.getView(position, null, content)) in a loop for all adapter positions (where content is the placeholder view you located with findViewById). Note that this is only practical if you know that you have a small number of list items in the adapter!

这篇关于我怎么能自动调整大小列表视图,因此它不会滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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