如何添加多个头的看法在ListView [英] How to add multiple header views in a ListView

查看:129
本文介绍了如何添加多个头的看法在ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的的ListView自定义适配器我要添加的项目名称作为标题,以我的工作请求。添加一个头工作得很好,但我不知道如何使用 addHeaderView 添加多个标题。我不明白的地方正好放置 setAdapter 还是它应该被放置多次?

I've a custom adapter for my ListView I want to add project names as the headers to my work requests. Adding a single header works just fine but I'm not sure how to add multiple headers using addHeaderView. I don't understand where exactly to place setAdapter or is it supposed to be placed multiple times?

这是我的Java $ C $下一个头它的工作原理:

This is my java code for a single header which works:

mListView = (ListView)findViewById(R.id.dashboardList);
View header1 =  getLayoutInflater().inflate(R.layout.listview_header, null, false);
tv = (TextView) header1.findViewById(R.id.listHeader);
adapter = new MyCustomAdapter(MyDashboardActivity.this, R.layout.mydashboard_row, dashboardBean);
tv.setText("Project 1");
mListView.addHeaderView(header1, null, false);
for (int i=0; i < 7; i++) {
     dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i]));
}
mListView.setAdapter(adapter);

现在,我有两个头我想这样的:

Now, I for two headers I tried this:

mListView = (ListView)findViewById(R.id.dashboardList);
View header1 =  getLayoutInflater().inflate(R.layout.listview_header, null, false);
tv = (TextView) header1.findViewById(R.id.listHeader);
adapter = new MyCustomAdapter(MyDashboardActivity.this, R.layout.mydashboard_row, dashboardBean);
tv.setText("RxOffice");
mListView.addHeaderView(header1, null, false);
for (int i=0; i < 4; i++) {
     dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i])); 
}

tv.setText(Project 2");

mListView.addHeaderView(header1, null, false);
for (int i=4; i < workRequests.length; i++) {
     dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i]));
}
mListView.setAdapter(adapter);

不过,这也不行!它给我的只有项目2头及其下面的所有7项。谁能告诉我什么是错的?我猜它是与 setAdapter 。谢谢!

推荐答案

我不认为你想要做什么是可能的,你正在尝试做的方式。当您使用 addHeaderView 它包装你的 ListAdapter HeaderViewListAdapter 。我看了看文档吧这里这似乎暗示着你可以有多个标题,但他们都将是在顶部(杜,头)。

I don't think what you want to do is possible the way you are trying to do it. When you use addHeaderView it wraps your ListAdapter in HeaderViewListAdapter. I looked at the docs for it here and that seems to imply that you could have multiple headers, but they would all be at the top (duh, header).

这听起来像你真正想要的是seperators ...

It sounds like what you actually want is seperators...

您可以使用 CommonWare的MergeAdapter 。它可以让你插入适配器和意见(以任何顺序你愿意的话)和present它们作为单个适配器列表视图。你只要把它头和适配器的内容的每个部分,然后将其加入列表。

You could use CommonWare's MergeAdapter. It will let you insert adapters and views (in whatever order you wish) and present them all as a single adapter to a listview. You just hand it headers and adapters for each section of content and then set it to your list.

伪code例如:

myMergeAdapter = new MergeAdapter(); 
myMergeAdapter.addView(HeaderView1); 
myMergeAdapter.addAdapter(listAdapter1); 
myMergeAdapter.addView(HeaderView2); 
myMergeAdapter.addAdapter(listAdapter2); 
setListAdapter(myMergeAdapter); 

这篇关于如何添加多个头的看法在ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆