2013-12-12 54 views
1

我有了一個LinearLayout中爲包含一個TextView和一個ListView像這樣初始化它的頁腳視圖一個ListView:的Android的ListView addFooterView保證金/填充問題

mylist = (ListView) findViewById(R.id.mylist); 
mylistadapter = new MyListAdapter(MainView.this, info); 
myfooterlistadapter = new MyListAdapter(MainView.this, info); 
LinearLayout footer = (LinearLayout)getLayoutInflater().inflate(R.layout.myfooter, null); 
myfooterlist = (ListView) footer.findViewById(R.id.myfooterlist); 
mylist.addFooterView(footer, null, false); 

mylist聲明爲:

<ListView 
    android:id="@+id/mylist" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" <!-- have also tried fill_parent without weight, same--> 
    android:layout_weight="1" 
    android:layout_marginLeft="5dip" 
    android:layout_marginRight="5dip" 
    android:cacheColorHint="#00000000" 
    android:dividerHeight="1dip" 
    android:drawSelectorOnTop="false" /> 

頁腳(LinearLayout中被聲明爲):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:id="@+id/myfooter" > 

    <TextView 
     android:id="@+id/myfooterlabel" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/headermainview" 
     android:paddingLeft="2dip" 
     android:text="@string/myfootertext" 
     android:textColor="@color/black" 
     android:textSize="16sp" 
     android:textStyle="bold" 
     android:visibility="gone" /> 

    <ListView 
     android:id="@+id/myfooterlist" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" <!-- have also tried fill_parent without weight, same--> 
     android:layout_weight="1" 
     android:cacheColorHint="#00000000" 
     android:dividerHeight="1dip" 
     android:drawSelectorOnTop="false" 
     android:visibility="gone" 
     android:background="@color/green" /> 

</LinearLayout> 

如果我頁腳列表包含項目,我將TextView和頁腳ListView上的可見性更改爲可見。

現在,我在頁腳列表視圖上繪製背景綠色以查看問題(遺憾地隱藏文本,但它是機密信息)。正如你所看到的,我在頁腳列表的底部不斷獲得額外的餘量。我嘗試從fill_parent改爲wrap_content爲0dip,列表和LinearLayout的weight = 1,但都沒有工作。如您所見,我沒有任何與在頁腳列表上的最後一項之後添加額外保證金相關的任何內容。 頂部列表中有20項(我已經滾動到屏幕的底部)和我的頁腳名單隻有在這種情況下,1項:

Extra margin on bottom of footer list

任何想法是怎麼回事,怎麼就不能有這在我的頁腳列表底部的額外餘量?謝謝!

UPDATE:

經進一步檢查,似乎無論第二列表的大小(我添加更多的項目看)的高度保持不變(綠框總是相同的大小)。我更改爲LinearLayout(無重量)和第二個列表上的fill_parent高度,但尺寸仍然相同。 ListView是否動態計算頁腳的高度以知道它有多大?

我添加了一個新的屏幕截圖,更詳細地顯示第二個項目被切斷(頂部列表未延伸到覆蓋整個第二個列表)。

1st list doesn't stretch to fully cover 2st list

更新2:

經過進一步調查,如果我把我的第2次的ListView(myfooterlist)的高度,以一個特定的高度(比如300dip),那麼第一個列表顯示了整個第二列表時,我滾動到底部。如果我將它設置爲fill_parent或wrap_conent或0dp並添加layout_weight = 1,那麼它會爲我提供與上面所有圖片相同的高度。任何想法如何動態計算高度?

+1

是否有任何特定的原因顯示第二個列表視圖爲頁腳?它可以對齊第一個列表視圖的底部..對吧? – Kameswari

+0

將頁腳部分保留爲獨立的佈局。同時從頁腳佈局中刪除第二個列表視圖。 – GrIsHu

+0

是的,我想滾動到第一個列表的底部,然後顯示TextView和頁腳列表。LinearLayout位於另一個名爲myfooter.xml的文件中。 – iliask

回答

0

我不認爲你可以在你的ListView的頁腳中嵌入第二個列表,我認爲這是你的問題的根源。在同一視圖(或屏幕)中嵌入多個可滾動視圖時常見問題。

將第二個列表視圖移出頁腳,我認爲它會正確顯示 - 我認爲您可以將其嵌入到頁腳中。

+0

請參閱更新2.當然,這將毫無疑問地工作,但與此問題是我必須修復第二個列表以採取特定大小的初始屏幕(滾動功能無法工作,因爲我將不得不包裝它在一個ScrollView中是不好的)。 – iliask

+0

這是特別針對所有的SO。問題在於,您無法在單個滾動視圖中顯示多個可滾動項目(如您的多個列表)。 (http://stackoverflow.com/questions/16515879/multiple-listviews-inside-a-scrollview)。 – Booger

+0

對於所有的打算和目的,我不想滾動第二個列表,因爲它是毫無意義的,因爲第一個列表將滾動瀏覽所有內容。所以從這個意義上說,它不像其他問題那樣需要在scrollview內部滾動listview。我試圖根據它的孩子動態地計算列表的高度,但它不是100%準確的,因爲並非所有項目都具有相同的高度。 – iliask