2013-11-29 59 views
0

我知道在這個主題上有很多Q & A,但我仍然沒有找到正確的答案。ListView顯示所有內容

我希望我的ListView(ScrollView內部)在屏幕底部邊緣「展開」,以顯示所有項目而不需要滾動條,因此將ListView放入ScrollView並不會造成問題,我們將能夠滾動整個活動。

LinearLayout中的Puting項目集合不是一個解決方案 - 它只是該死的慢。繪製100個項目需要2-3秒,而ListView會立即執行。

回答

0

您可以將ListViewlayout_height設置爲wrap_content,但那麼它會和LinearLayout一樣慢。 ListView整點是它的內容是假的;它只會創建在其高度內可見的行。使其與內容一樣高可以消除其優勢。

我建議重新考慮你想要做什麼。也許你可以把所有的小工具放在ListView之內,然後你就不需要ScrollView

+0

關於重新思考:)我需要顯示除了列表中的某些數據集ListView的在運行時的高度。這些數據需要垂直空間。如果我希望我的用戶儘可能多地查看列表,我必須從屏幕上刪除其他數據。最好的辦法就是向下滾動。以這種方式,我確定用戶需要什麼 - 查看List。我真的不知道是否有其他方式來實現這一點。 – ozapa

+0

你嘗試過'ListView.addFooterView'嗎? –

+0

請upvote /接受如果這可以解決您的問題。 –

0

你可以嘗試使用這個片段

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); 
      if (listItem instanceof ViewGroup) 
       listItem.setLayoutParams(new LayoutParams(
         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
      listItem.measure(0, 0); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 

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

而且例如使用此

listComment.setAdapter(new CommentAdapter(comment, 
         AppDetailActivity.this)); 
       listComment.postDelayed(new Runnable() { 

        @Override 
        public void run() { 
         // TODO Auto-generated method stub 
         Utils.setListViewHeightBasedOnChildren(listComment); 

        } 
       }, 500);