from:http://daydayup1989.iteye.com/blog/905880
在ScrollView中加入ListView時,有個很棘手的BUG,就只能顯示出ListView的一行半左右。
ListView 本來是不應該 放在 ScrollView 裏的,Google員工 Roman Guy早已回覆:
"There is no need to put a ListView in a ScrollView since ListView already supports scrolling. Do NOT put a ListView inside a ScrollView. ListView already handles scrolling, you’re only going to run into trouble. "
我當然是不想自找麻煩,把ListView往ScrollView裏面加的,但是項目中有這個需求。
大致發現有以下幾種方法:
法一:
一個Activity,其中不只有ListView一個控件,還有其他的TextView、ImageView、Button等等很多控件,這樣很可能會 佔據屏幕很大一部分,要知道手機的屏幕只有480像素,通常的想法是給這個Activity加上一個ScrollView,讓其有滾動條,但是會發現 ListView的高度不是隨着內容而自動填充的。那麼我們可以使用ListView的addHeaderView 以及addFooterView 爲ListView增加上下的頭和尾,這樣就可以讓ListView填充到整個屏幕。
步驟:
1) 新建一個Layout: demo_list_item_header_view.xml:
- <linearlayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <textview
- android:text="TestListViewHeader"
- android:id="@+id/headerTextView"
- android:textsize="20sp"
- android:layout_width="wrap_content"
- android:layout_height="30sp">
- </textview>
- </linearlayout>
2) 新建一個類,繼承自LinearLayout用來顯示上面的Layout:
DemoListHeaderView.java
- package com.zhang.test.view;
- import com.zhang.test.R;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class DemoListHeaderView extends LinearLayout {
- private static final String TAG = "DemoListHeaderView";
- private Context context;
- private TextView textView;
- public DemoListHeaderView(Context context, AttributeSet attrs) {
- super(context, attrs);
- initialize(context);
- }
- public DemoListHeaderView(Context context) {
- super(context);
- initialize(context);
- }
- private void initialize(Context context) {
- this.context = context;
- View view = LayoutInflater.from(this.context).inflate(R.layout.demo_list_item_header_view, null);
- textView = (TextView) view.findViewById(R.id.headerTextView);
- addView(view);
- }
- public void setTextView(String text) {
- textView.setText(text);
- }
- }
之後在ListView設置setAdapter之前,一定要在setAdapter之前
加上代碼:
- DemoListHeaderView headerView = new DemoListHeaderView(context);
- headerView.setTextView("Header : ");
- listView.addHeaderView(headerView);
- DemoListHeaderView footerView = new DemoListHeaderView(context);
- footerView.setTextView("Footer : ");
- listView.addFooterView(footerView);
全部:
- package com.zhang.test;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.widget.ListView;
- import com.zhang.test.view.DemoListHeaderView;
- import com.zhang.test.view.DemoListItemView;
- import com.zhang.test.view.adapter.DemoListAdapter;
- public class demoActivity extends Activity {
- private static final String TAG = "demoActivity";
- private Context context;
- private ListView listView;
- private ArrayList<demolistitemview.data> datas;
- private DemoListAdapter datasAdapter;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- context = this;
- listView = (ListView) findViewById(R.id.listView);
- datas = new ArrayList<demolistitemview.data>();
- loadData();
- datasAdapter = new DemoListAdapter(context, datas);
- DemoListHeaderView headerView = new DemoListHeaderView(context);
- headerView.setTextView("Header : ");
- listView.addHeaderView(headerView);
- DemoListHeaderView footerView = new DemoListHeaderView(context);
- footerView.setTextView("Footer : ");
- listView.addFooterView(footerView);
- listView.setAdapter(datasAdapter);
- }
- private void loadData() {
- DemoListItemView.Data d;
- for(int i=0; i<10; i++) {
- d = new DemoListItemView.Data();
- d.topText = "測試top";
- d.bottomText = "測試bottom";
- datas.add(d);
- }
- }
- }
- </demolistitemview.data></demolistitemview.data>