2010-08-17 69 views
8

目前,我有如下的布局我如何能自动调整大小列表视图,因此不会滚动

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

,我已经注释掉列表视图,我试图让这个在XML中,以高度设定到WRAP_CONTENT,FILL_PARENT,目前我与编程下面的代码

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

这这样做是结果

alt text

alt text

这样你就可以在这个图像中看到的,因为我包含在LinearLayout中的列表视图,以获得圆角的外观,它不只是自动延伸到包含整个列表视图,没有任何如何让这两个元素垂直包装内容,以便在没有编程设置高度的情况下不会滚动? ? ?

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

-scrollview
-textview
-textview

-linearlayout
-listview

- 按钮

这里是我有什么

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

回答

18

列表视图不ScrollViews去一个简单的布局。

ListView用于有效地显示无限制内容的有限窗口。如果您要在ListView上“禁用滚动”以将其放置在ScrollView中,则首先会失去使用ListView的所有实际原因。

如果您想要使用ListView显示大量内容或无限内容,但也有上下滚动的内容,请使用addHeaderViewaddFooterView向ListView添加页眉或页脚视图。如果列表内容将只是您描述的整体布局的一小部分,那么这对您来说可能不是最好的方法。

如果您有一个小的,有界的内容集,请继续使用ScrollView并在适当情况下以编程方式为您的“列表项”生成子视图。

在框架用于混合通过编程生成的内容膨胀XML内容的常见模式是在布局XML添加占位符视图,通常是LinearLayoutFrameLayout。使用findViewById在运行时找到它并向其添加生成的子视图。

你甚至可以仍然使用ListAdapter这种方法,如果你已经写了一个,只需要调用content.addView(adapter.getView(position, null, content))在一个循环中的所有适配器的位置(其中content是你所在与findViewById占位符视图)。请注意,只有在知道适配器中有少量列表项的情况下,这才是实用的!

+0

感谢亚当,我已经有一个适配器,我能够与使用的LinearLayout您提示重新使用。 – 2011-06-01 22:26:31

+0

谢谢!与Artem相同,我可以使用线性布局重新使用我的适配器。 – hyui 2011-11-07 15:45:23

+0

谢谢亚当。你的适配器是否需要异步?当我尝试这个时,它只抓住我的第一个项目,就像适配器没有完成加载。 – annie 2012-09-10 18:11:45

0

在列表末尾添加一个空项目

例子:

ArrayList<String> options = new ArrayList<String>(); 
String lastItem = ""; 
int lastPosition; 

options.add(lastItem); 

public function addItem() { 
    lastPosition = options.size() - 1; 
    lastItem = options.get(lastPosition); 
    options.remove(lastPosition); 

    //add new items dynamically 
    for (int i = 0; i < 5; i++) 
     options.add("new item: "+i); 

    //add empty item 
    options.add(lastItem); 
} 
相关问题