2013-10-01 28 views
4

我有ExpandableListviewScrollView,我知道那不是很好,但我有太多,顯示整個列表中的唯一的解決辦法是通過設定使用layoutParams機器人組列表視圖高度動態

RelativeLayout.LayoutParams其通過代碼高度params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, ListViewData.length());

這個解決方案是好的,但我不認爲我應該在Params給合適的高度,那麼,有沒有辦法知道從數組

編輯的大小實際尺寸: 我想出了,每次我展開一個組列表時的解決方案會改變高度以適應新的geight

+0

嘿,你會寫你的解決方案嗎? – yshrsmz

回答

0

可以設置在尺寸可變的文件爲不同的屏幕尺寸,然後由數乘以您想要顯示的列表項目。

+0

我試圖使用一個固定的變量,然後乘以項目的數量,但它不在不同的屏幕尺寸 – alaa7731

+0

@ alaa7731這就是我所說的,你需要爲不同的屏幕尺寸設置不同的值。 – Pallavi

13

試試這個,基於listview使用Child。 setListViewHeightBasedOnChildren()這將設置你的孩子的ListView基於高度

public class Utils { 

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); 
    listView.requestLayout(); 
} 



} 
+0

mmmmmmmm好吧,我現在試試吧 – alaa7731

+4

不工作,高度比它應該大 – alaa7731

+1

它的工作,但你應該添加這些解決方案的來源。 – Kailas

8

你的ListView在這種情況下有效不必要的。您還可以循環使用適配器的項目,並將它們添加到ScrollView中的垂直LinearLayout。

如果你不想改變很多代碼:

LinearLayout ll; //this should be the vertical LinearLayout that you substituted the listview with 
for(int i=0;i<adapter.getCount();i++) { 
    View v = adapter.getView(position, null, null); 
    ll.addView(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
} 

更換ListView.setAdapter如果您已經使用了OnItemClickListener View v = adapter.getView(position, null, null);後添加以下

final int position = i; 
v.setOnClickListener(new OnClickListener() { 
    public onClick(View v) { 
     yourOnItemClickListener.onItemClick(null, v, position, 0); 
    } 
}); 

在這種情況下,您不必擔心abou t高度的任何錯誤計算。

+0

我想出了listview的情況下的這個解決方案,但現在它會改爲ExpandableListView:/,所以在這種情況下,它也必須動態地完成 – alaa7731

+0

我認爲你應該爲該編輯創建一個新問題。你特別要求一個listview。 –

+0

是的,我想:/,無論如何感謝 – alaa7731

3

試試這個,它適合我同樣的問題

public static boolean setListViewHeightBasedOnItems(ListView listView) { 

ListAdapter listAdapter = listView.getAdapter(); 
if (listAdapter != null) { 

    int numberOfItems = listAdapter.getCount(); 

    // Get total height of all items. 
    int totalItemsHeight = 0; 
    for (int itemPos = 0; itemPos < numberOfItems; itemPos++) { 
     View item = listAdapter.getView(itemPos, null, listView); 
     item.measure(0, 0); 
     totalItemsHeight += item.getMeasuredHeight(); 
    } 

    // Get total height of all item dividers. 
    int totalDividersHeight = listView.getDividerHeight() * 
      (numberOfItems - 1); 

    // Set list height. 
    ViewGroup.LayoutParams params = listView.getLayoutParams(); 
    params.height = totalItemsHeight + totalDividersHeight; 
    listView.setLayoutParams(params); 
    listView.requestLayout(); 

    return true; 

} else { 
    return false; 
}} 

requestLayout(),因爲事情已經改變了無效的佈局方法被調用視圖 - 強制重繪。

+0

** NPE **在item.measure(0,0);'拋出。 – SilentKnight