在ListActivity
中,我在列表下方有一個「更多」Button
。Android:將項目動態添加到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
我期望按鈕留在其位置(我指的是最後一項下面列表中的)
感謝您的幫助......
謝謝,但有關於此的警告:「layout_weight」在RelativeLayout中無效的佈局參數:layout_weight –