想給listview的上方加一個header,我們可以 在listview上方增加布局來實作 如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.testlistviewscrollview.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dip"
android:text="-----這裡是标題--------"
android:textColor="@android:color/holo_red_dark" />
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@android:color/holo_blue_bright"
android:dividerHeight="5dip"/>
</LinearLayout>
但是,需要注意的是,這中做法,listview的header是固定在listview上的,不能随着listview的滑動而滑動。
如果,我們的需求是:header随着listview的滑動而滑動呢?
這個時候,我們或許就會用到ScrollView,在ScrollView裡嵌套一個listview來實作我們的需求
如”
xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.testlistviewscrollview.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"><!--這裡為什麼又嵌套了一層布局呢?不說了。如果不知道,自己去掉這層view再運作看看-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dip"
android:text="-----這裡是标題--------"
android:textColor="@android:color/holo_red_dark" />
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@android:color/holo_blue_bright"
android:dividerHeight="5dip" />
</LinearLayout>
</ScrollView>
</LinearLayout>
java類:
public class MainActivity extends Activity {
private ListView listView;
private TestAdapter adapter;
private List<String> data;
int n = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
data = getData();
adapter = new TestAdapter(this, data);
listView.setAdapter(adapter);
setListener();
}
private List<String> getData() {
List<String> data = new ArrayList<String>();
for (int i = 2; i < 20; i++) {
data.add("第" + n + "頁 測試資料" + i);
}
n++; // 用來模拟分頁加載
return data;
}
}
這樣的話産生了一個問題:你在螢幕上看到的listview隻顯示一個條目,如何解決這個問題呢?
1.xml中 寫死listview的高度。經過測設,這種方法也具有一定的可行性,但是它還有個弊端,就是listview的資料是可變動的,除非你能正确的寫出listview的高度。
2. 在代碼中動态計算listview的高度:在給listview設定完adapter之後,再動态的設定listview的高度。
在 listView.setAdapter(adapter);之後調用 Util.setListViewHeight(listView);
setListViewHeight:
public static void setListViewHeight(ListView listview) {
ListAdapter listAdapter = listview.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listview);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
int aa = listAdapter.getCount();
ViewGroup.LayoutParams params = listview.getLayoutParams();
params.height = totalHeight + (listview.getDividerHeight() * (aa - 1));
// ((MarginLayoutParams) params).setMargins(0,0,0, 0);
listview.setLayoutParams(params);
}
運作後發現,雖然目前是打到我們的需求了,但是還存在幾個問題:
1》在剛打開頁面的是時候,界面自動定位到listview的第0條目,listview的header是看不到的
2》分頁加載加載第二頁的時候,listview的高度不更新
時間緊,這倆個問題後續-------
另外 提醒大家注意一下:之前我看到網上好多文章說 用 動态設定listview的高度這種方法時,要求adapter的xml布局裡必須要有LinearLayout,還說出了原因。之前我就年輕的相信了,。後來我寫代碼驗證了一下,這種說法是不正确的,我用的是相對布局,也是可以的。也許,網上的那種說法隻是在某些特定的情況下才成立。
是以,提醒大家,不要輕易相信别人:),一定要自己驗證