首相先在gradle里引入PinnedHeaderListView
和普通的listview一样,只不过分组。一个头部一组数据。 如果 你要用直接把下面的代码拷入;
class ApprovalSectionedAdapter extends SectionedBaseAdapter implements PinnedHeaderListView.PinnedSectionedHeaderAdapter {
private List<String> headList = new ArrayList<String>();//这里是头部的集合
private List<List<Communityroom>> approvalMap = new ArrayList();//这里是每个头部下数据的分组的数据结合。注Communityroom是我的一个类;
public ApprovalSectionedAdapter(List<String> headList, List<List<Communityroom>> approvalMap) {
this.headList = headList;
this.approvalMap = approvalMap;
}
@Override
public Object getItem(int section, int position) {//得到每个分组的单个数据(不包含头部)
return approvalMap.get(section).get(position);
}
@Override
public long getItemId(int section, int position) {
return position;
}
@Override
public int getSectionCount() {//有多少个头部分组
Log.e("[ZOZO]", "headList.size()--->" + headList.size());
return headList.size();
}
@Override
public int getCountForSection(int section) {//头部下总的数据;
List<Communityroom> contentList = approvalMap.get(section);
Log.e("[ZOZO]", "contentList.size()--->" + contentList.size());
return contentList.size();
}
@Override
public View getItemView(final int section, int position, View convertView, ViewGroup parent) {//加载头部下的分组布局
LinearLayout layout = null;
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (LinearLayout) inflator.inflate(R.layout.attenttionadapter2, null);
} else {
layout = (LinearLayout) convertView;
}
final Communityroom contentMap = approvalMap.get(section).get(position);
layout.setTag(contentMap);//这里个listview一样哦。下面是加载我的东西。你可以把你的东西加上。
/* final String content = contentMap.present;
ImageView avatar = (ImageView) layout.findViewById(R.id.avatar);
TextView name = (TextView) layout.findViewById(R.id.title);
TextView contentview = (TextView) layout.findViewById(R.id.industry);
TextView numble = (TextView) layout.findViewById(R.id.number);
name.setText(contentMap.name);
numble.setText(contentMap.count + "人");
contentview.setText(contentMap.present + "fff");
String uri2 = WebMgr.communityPicPath(contentMap.avatar);
ImageLoader.getInstance().displayImage(uri2, avatar, ImageLoadTool.optionsAvatarnodefault);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent il = new Intent(MycommunityActivity.this, L0_CommunityActivity.class);
il.putExtra(L0_CommunityActivity.Result_type,1);
il.putExtra("gid",contentMap.gid);
startActivity(il);
}
});*/
return layout;
}
@Override
public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {//加载头部的布局
LinearLayout layout = null;
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (LinearLayout) inflator.inflate(R.layout.g0_3_approval_template_section_view, null);
} else {
layout = (LinearLayout) convertView;
}
layout.setTag(headList.get(section));
int size = contentMapList.get(section).size();
String title = headList.get(section);
((TextView) layout.findViewById(R.id.textItem)).setText(title + "(" + size + ")");
return layout;
}
}