2009-11-22 69 views
29

我有一點困難,同時試圖讓一定的佈局工作:我想有名單。列表不必滾動,但應完全顯示。但是,如果總頁面內容高於屏幕,頁面本身應該能夠滾動(使用列表)。android listview顯示所有可用的項目沒有滾動與靜態標題

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/linear_layout" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="#ff181818" 
     > 
      <Textview android:id="@+id/my_text" text="header contents goes here" android:layout_width="fill_parent" android:layout_height="wrap_content"/> 
      <Textview android:id="@+id/headertext" text="header contents goes here" android:layout_width="fill_parent" android:layout_height="wrap_content"/> 

      <ListView 
       android:id="@+id/my_list1" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent" 
      /> 
    </LinearLayout> 

</ScrollView> 

它僅使用屏幕的一小部分(每列表約2行),而不是填充可用的高度,以及清單自身可以滾動。我怎樣才能改變佈局總是顯示整個列表,但讓屏幕滾動?

+0

http://www.anddev.org/viewtopic.php?p=25194 在另外一個論壇上,我發現了一些評論的東西像這樣 「,因爲一個ListView已經有滾動的能力。因此,我已經重寫我的自定義列表繼承自LinearLayout,它在ScrollView中工作正常,我不知道這是否是唯一的方法,但它實現了我現在想要做的事情。「 你能幫我解決以上情況嗎? – 2009-11-22 19:26:41

+0

..請你給我們提供一些代碼和xml佈局,你可以在其中實現整個屏幕滾動...謝謝... – 2011-06-22 17:26:30

回答

60

我使用的解決方案是用LinearLayout替換ListView。您可以在LinearLayout內創建所有項目,它們都將顯示。所以真的不需要使用ListView。

LinearLayout list = (LinearLayout)findViewById(R.id.list_recycled_parts); 
for (int i=0; i<products.size(); i++) { 
    Product product = products.get(i); 
    View vi = inflater.inflate(R.layout.product_item, null); 
    list.addView(vi); 
} 
+0

請你詳細說明你在這裏做了什麼.. – 2011-06-22 17:21:53

+0

Shouldn你是否把物體傳遞給膨脹? – dierre 2012-03-11 21:50:37

+0

什麼是R.layout.product_item? – Ken 2012-08-30 12:24:24

-6

在你的LinearLayout設置android:layout_height="fill_parent"

+1

不,不幸的是,這不會幫助 - ListView將始終嘗試滾動其內容。所以儘管它不如ListView好,但LinearLayout是這裏的一種方式。 – 2011-09-21 23:38:05

1

檢查了這一點:

ListView ignoring wrap_content

採用Android:layout_height和android:layout_weight解決了這個問題對我來說:

<ListView 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    /> 
+0

對我不起作用 – 2016-04-26 01:00:11

+0

對我來說不起作用:( – Sam 2017-06-27 14:08:50

1

我只是使用Lis設置參數tView

public static void setListViewHeightBasedOnChildren(ListView listView) { 

    //this comes from value from xml tag of each item 
    final int HEIGHT_LARGE=75; 
    final int HEIGHT_LARGE=50; 
    final int HEIGHT_LARGE=35; 
    ViewGroup.LayoutParams params = listView.getLayoutParams(); 

    int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; 

    switch(screenSize) { 

    case Configuration.SCREENLAYOUT_SIZE_LARGE: 
     params.height =(int) (HEIGHT_LARGE*size); 
     break; 
    case Configuration.SCREENLAYOUT_SIZE_NORMAL: 
     params.height =(int) (HEIGHT_NORMAL*size); 
     break; 
    case Configuration.SCREENLAYOUT_SIZE_SMALL: 
      params.height =(int) (HEIGHT_SMALL*size); 
      break; 
    } 
    listView.setLayoutParams(params); 
} 
+0

listview有使用適配器的優點。使用線性佈局並不實現這個便捷的功能。 – Ivan 2014-12-04 08:43:34

+2

你可以使用listview不僅僅是線性佈局 – Ivan 2014-12-07 20:17:18

11

我在佈局了ListView,想使用,因爲它把它包裝成一個ScrollView無法處理ListView這裏的圖書。對我來說最好的解決方案是基於Fedor的答案。

因爲我已經有了一個ArrayAdapterListView我想重新使用它:

LinearLayout listViewReplacement = (LinearLayout) findViewById(R.id.listViewReplacement); 
NamesRowItemAdapter adapter = new NamesRowItemAdapter(this, namesInList); 
for (int i = 0; i < adapter.getCount(); i++) { 
    View view = adapter.getView(i, null, listViewReplacement); 
    listViewReplacement.addView(view); 
} 

對於我來說,因爲我只需要顯示動態變化的數據從1到5個元素能正常工作。我只需要添加我自己的分頻器。

+1

這是最好的想法,使所有可見項目的靜態列表 – 2016-07-14 09:49:32

23

正如@Alex在接受的答案中指出的那樣,LinearLayout幾乎不是替代品。我有一個問題,LinearLayout不是一個選項,那是當我遇到這個blog。我將把代碼放在這裏供參考。希望它能幫助那裏的人!

public class UIUtils { 

    /** 
    * Sets ListView height dynamically based on the height of the items. 
    * 
    * @param listView to be resized 
    * @return true if the listView is successfully resized, false otherwise 
    */ 
    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; 
     } 

    } 
} 

用法:

//initializing the adapter 
listView.setAdapter(adapter); 
UIUtils.setListViewHeightBasedOnItems(listView); 

//whenever the data changes 
adapter.notifyDataSetChanged(); 
UIUtils.setListViewHeightBasedOnItems(listView); 
+0

這個問題的最佳解決方案:) – 2016-08-03 16:03:21

+2

工程像一個魅力,但不應該 UIUtils.setListViewHeightBasedOnItems(適配器);是 UIUtils.setListViewHeightBasedOnItems(listView); ? – Reyske 2016-09-06 08:55:48

+0

非常感謝你:) – iwooli 2016-09-13 15:41:00

5

你可以使自己的customlistview。 (它可以擴展ListView/ExpandableListView/GridView)並用此替代onMeasure方法。有了這個,你永遠不需要調用一個函數或任何東西。只需在你的xml中使用它。

@Override 
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, 
      MeasureSpec.AT_MOST); 
    super.onMeasure(widthMeasureSpec, expandSpec); 
} 
+0

這是一個更好的答案,因爲它是一個可擴展和乾淨的方法。 – 2017-02-07 17:19:15

+0

它看起來很乾淨。我想知道這個我們可以在scrollview中放置listview嗎? – anhtuannd 2017-04-19 05:23:00

+1

@anhtuannd我沒有嘗試過......但唯一改變的是onMeasure方法,ListView的所有滾動方法仍然可用,但沒有任何滾動。所以如果listview在scrollview中,我認爲它仍然會覆蓋這些方法。我會尋找另一種方法來做到這一點。 – Francisco 2017-04-20 14:27:07

0

如果所有項目具有相同的高度

 int totalItemsHeight = baseDictionaries.size() * item.getMeasuredHeight(); 
     int totalDividersHeight = listView.getDividerHeight() * (baseDictionaries.size() - 1); 
     int totalPadding = listView.getPaddingBottom() + listView.getPaddingTop(); 

     LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) listTranslationWords.getLayoutParams(); 
     lp.height = totalItemsHeight + totalDividersHeight + totalPadding; 
     listTranslationWords.setLayoutParams(lp); 
0

蔭supprised沒有人看到this.U不能有相同的佈局兩個渦旋盤。第一你有一個滾動視圖,然後你有一個列表,我敢打賭,你在那裏殺了一些Android的良好做法。

1

如果有人仍然有問題,那麼你可以讓customList並添加onMesure()方法就像我實現它:

​​
0

如果你想有一個簡單的解決這個問題,而擴展的ListView類,這是一個解決方案爲你。

mListView.post(new Runnable() { 
     @Override 
     public void run() { 
      int height = 0; 
      for(int i = 0; i < mListView.getChildCount();i++) 
       height += mListView.getChildAt(i).getHeight(); 
      ViewGroup.LayoutParams lParams = mListView.getLayoutParams(); 
      lParams.height = height; 
      mListView.setLayoutParams(lParams); 
     } 
    }); 
相關問題