ListView 适配器的页眉和页脚
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
resultString = sb.toString();
String[] ArrayStr = Func.split(resultString, "|*|");
String[] arr;
lv = new ListView(context);
lv.setAdapter(null);
// populate
ArrayList<Device> m_Devices = new ArrayList<Device>();
Device device;
for(int i = 0; i < ArrayStr.length; i++){
arr = Func.split(ArrayStr[i], "|**|");
device = new Device(arr[1], Integer.parseInt(arr[0]));
m_Devices.add(device);
}
// +header n footer (must above custom adapter)
View v1 = getLayoutInflater().inflate(R.layout.header, null);
lv.addHeaderView(v1);
View v = getLayoutInflater().inflate(R.layout.footer, null);
lv.addFooterView(v);
// custom adapter
CustomAdapter lvAdapter = new CustomAdapter(context, m_Devices);
lv.setAdapter(lvAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(Menu1.this);
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = "+lv.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
如何使页眉和页脚变为静态? 页眉和页脚也可以滚动,就像列表视图一样。 我有谷歌,有人建议 4 将页眉和页脚放在列表视图之外,但我不知道放在哪里?我已经尝试将它放置到我的自定义适配器中,但没有什么好处...所以有人可以告诉我如何解决这个问题吗?
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
resultString = sb.toString();
String[] ArrayStr = Func.split(resultString, "|*|");
String[] arr;
lv = new ListView(context);
lv.setAdapter(null);
// populate
ArrayList<Device> m_Devices = new ArrayList<Device>();
Device device;
for(int i = 0; i < ArrayStr.length; i++){
arr = Func.split(ArrayStr[i], "|**|");
device = new Device(arr[1], Integer.parseInt(arr[0]));
m_Devices.add(device);
}
// +header n footer (must above custom adapter)
View v1 = getLayoutInflater().inflate(R.layout.header, null);
lv.addHeaderView(v1);
View v = getLayoutInflater().inflate(R.layout.footer, null);
lv.addFooterView(v);
// custom adapter
CustomAdapter lvAdapter = new CustomAdapter(context, m_Devices);
lv.setAdapter(lvAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(Menu1.this);
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = "+lv.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
How to make the header and footer become static?
the header n footer become scrollable too, like the list view..
i had google n there is someone sugest 4 placing the header and footer outside the listview, but i don't know place to where ? i'm already try place it to my custom adapter n there's no good... so plz anybody can tell me how to fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为将它们放在 ListView 之外意味着您应该使用自定义视图元素,例如放置在 ListView 上方和下方的 TextView。这些元素与 ListView 无关。因此,您无法再使用
lv.addHeaderView(v1);
或lv.addFooterView(v);
访问它们。相反,您必须像普通 TextView 元素一样对待它们。当然,您也可以使用自定义视图来代替 TextView。更新
您的活动的布局应该类似于这样。第一个 TextView 是页眉,后面是 ListView,下面是另一个 TextView 作为页脚。页眉和页脚不需要 TextView,它们可以是任何类型的 View。
因此,您将像之前一样将适配器绑定到 ListView。要设置页眉和页脚的文本,请使用以下行。
I think placing them outside of the ListView means that you should use custom view elements e.g. TextViews which you place above and below the ListView. These elements have nothing to do with the ListView anymore. So you cannot access them with
lv.addHeaderView(v1);
orlv.addFooterView(v);
anymore. Instead you have to treat them like normal TextView elements. Of course you can also use custom Views instead of TextViews.Update
The layout for you activity should look something like that. The first TextView is the header, followed by the ListView and below this we got another TextView as a footer. Header and footer don't need to TextViews they can be any kind of View.
So you will bind the adapter to the ListView as you already did. To set the text for the header and footer you use the following lines.