ListView与标题

时间:2013-08-21 21:28:05

标签: android listview adapter

有没有办法在View的{​​{1}}上方插入另一个List?例如,我想要一个位于ListView顶部的标题栏(TextView),并在列表滚动时滚动出视图。到目前为止,我可以想到两种方法,但两者都是黑客攻击。

想法#1 - 使用假装为ListView的LinearLayout。但问题是您无法利用适配器提供的“智能”加载/卸载视图。

List

想法#2 - 使用hacky <ScrollView> <LinearLayout> <TextView/> <LinearLayout android:orientation="vertical" /> # add ListItems here </LinearLayout> </ScrollView> ,使用与此类似的ArrayAdapter方法:

getView

1 个答案:

答案 0 :(得分:37)

简单。只需在ListView

中添加标题视图即可
TextView textView = new TextView(context);
textView.setText("Hello. I'm a header view");

listView.addHeaderView(textView);