2014-05-04 56 views
1

ListActivity中,我在列表下方有一個「更多」ButtonAndroid:將項目動態添加到ListView中而不會混亂

當我點擊它,結果添加到列表中正確,但問題是該按鈕後,這個消失!我希望它留在那裏,用戶當然可以再次加載更多的數據!

的代碼是:

btnMoreItems.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // some fake data 
      for (int i = 10; i < 20; i++) { 
       data.add(// fake data to attach to the list //); 
      } 

      setListAdapter(new MyCustomArrayAdapter(MyListActivity.this, data)); 
     } 
    }); 

和佈局是一些這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ListView 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

    <TextView 
     android:id="@+id/android:empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/myListEmpty" /> 

    <Button 
     android:id="@+id/btnMoreItemsLoad" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
    → → android:layout_below="@+id/android:list ← ← ← ← ← 
     android:text="@string/moreItems" /> 

</RelativeLayout> 

當第一次活性負載,按鈕是在正確的位置。但點擊後,它會消失在添加的項目後面。

這一行:android:layout_below="@+id/android:list我期望按鈕留在其位置(我指的是最後一項下面列表中的)

感謝您的幫助......

回答

0

更改佈局到以下:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ListView 
     android:align_parentTop="true" ← ← ← ← ← 
     android:layout_weight="1" ← ← ← ← ← 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" > 
    </ListView> 

    <TextView 
     android:id="@+id/android:empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/myListEmpty" /> 

    <Button 
     android:layout_weight="0" ← ← ← ← ← 
     android:align_parentBottom="true" ← ← ← ← ← 
     android:id="@+id/btnMoreItemsLoad" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/android:list 
     android:text="@string/moreItems" /> 

</RelativeLayout> 
+0

謝謝,但有關於此的警告:「layout_weight」在RelativeLayout中無效的佈局參數:layout_weight –

0

試試這個代碼

<TextView 
     android:id="@+id/android:empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/myListEmpty" /> 

    <ListView 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/android:empty" 
     android:layout_marginTop="5dp" > 
    </ListView> 

    <Button 
     android:id="@+id/btnMoreItemsLoad" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/android:list" 
     android:text="@string/moreItems" /> 

</RelativeLayout> 
+0

謝謝......但是對我的代碼沒有太大的改變!您只向ListView添加「layout_marginTop =」5dp「」。你確定這個解決方案!?我試試這個,它不是解決問題... –

+0

不僅上邊距,我高增加布局:下面在列表視圖也..因爲我的理解它shl工作 – Meghna

+0

我試試這個,但不工作!實際上其中只有一個(ListView&TextBox)顯示!如果列表中沒有項目,ListView被隱藏,否則如果列表不是空的,TextBox將被隱藏! –