If you think the Android project Munin-for-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.chteuchteu.munin.adptr;
/*www.java2s.com*/import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.chteuchteu.munin.R;
import com.chteuchteu.munin.hlpr.Util;
import com.chteuchteu.munin.hlpr.Util.Fonts.CustomFont;
import java.util.LinkedHashMap;
import java.util.Map;
publicclass Adapter_SeparatedList extends BaseAdapter {
private Context context;
privatefinal Map<String,Adapter> sections;
privatefinal ArrayAdapter<String> headers;
privatefinalstaticint TYPE_SECTION_HEADER = 0;
privateboolean removeMasterPaddingLeft;
public Adapter_SeparatedList(Context context, boolean removeMasterPaddingLeft) {
this.sections = new LinkedHashMap<>();
this.headers = new ArrayAdapter<>(context, R.layout.list_header);
this.context = context;
this.removeMasterPaddingLeft = removeMasterPaddingLeft;
}
publicvoid addSection(String section, Adapter adapter) {
this.headers.add(section);
this.sections.put(section, adapter);
}
public Object getItem(int position) {
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return section;
if(position < size) return adapter.getItem(position - 1);
// otherwise jump into next section
position -= size;
}
return null;
}
publicint getCount() {
// total together all sections, plus one for each section header
int total = 0;
for(Adapter adapter : this.sections.values())
total += adapter.getCount() + 1;
return total;
}
publicint getViewTypeCount() {
// assume that headers count as one, then total all sections
int total = 1;
for(Adapter adapter : this.sections.values())
total += adapter.getViewTypeCount();
return total;
}
publicint getItemViewType(int position) {
int type = 1;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return TYPE_SECTION_HEADER;
if(position < size) return type + adapter.getItemViewType(position - 1);
// otherwise jump into next section
position -= size;
type += adapter.getViewTypeCount();
}
return -1;
}
publicboolean areAllItemsSelectable() {
return false;
}
publicboolean isEnabled(int position) {
return (getItemViewType(position) != TYPE_SECTION_HEADER);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int sectionnum = 0;
for (Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if (position == 0) {
View view = headers.getView(sectionnum, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.list_header_title);
Util.Fonts.setFont(context, textView, CustomFont.Roboto_Medium);
if (removeMasterPaddingLeft)
textView.setPadding(55, textView.getPaddingTop(), textView.getPaddingRight(), textView.getPaddingBottom());
return view;
}
if (position < size)
return adapter.getView(position - 1, convertView, parent);
// otherwise jump into next section
position -= size;
sectionnum++;
}
return null;
}
@Override
publiclong getItemId(int position) {
return position;
}
}