2010-08-17 71 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); 
} 
相關問題