I got several header scripts using the custom Section Adapter
that was originally encoded by CommonsWare , you can make a section in the list, for example Books, Games, etc. check the code below.
Section adapter:
package com.medplan.db; import java.util.ArrayList; import java.util.List; import android.view.View; import android.view.ViewGroup; import android.widget.Adapter; import android.widget.BaseAdapter; abstract public class SectionedAdapter extends BaseAdapter { String TAG = "========SectionedAdapter============"; abstract protected View getHeaderView(String caption, int index, View convertView, ViewGroup parent); private List<Section> sections=new ArrayList<Section>(); private static int TYPE_SECTION_HEADER=0; public SectionedAdapter() { super(); sections.clear(); } public void addSection(String caption, Adapter adapter) { sections.add(new Section(caption, adapter)); } public void clear() { sections.clear(); notifyDataSetChanged(); } public Object getItem(int position) { for (Section section : this.sections) { if (position==0) { return(section); } int size=section.adapter.getCount()+1; if (position<size) { return(section.adapter.getItem(position-1)); } position-=size; } return(null); } public int getCount() { int total=0; for (Section section : this.sections) { total+=section.adapter.getCount()+1;
Inside the action, make the section adapter object, see the code below:
final SectionedAdapter adapter =new SectionedAdapter() { protected View getHeaderView(String caption, int index, View convertView,ViewGroup parent) { result=(TextView)convertView; if (convertView==null) { result=(TextView)getLayoutInflater().inflate(R.layout.section_header,null); } result.setText(caption);
section_header.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/black" android:textColor="#FFFFFF" android:ellipsize="end" android:textSize="11sp" style="?android:attr/listSeparatorTextViewStyle" />
In the "Activity" section, add the section as much as you want:
Note: userPic and medPic are the name of arraylist.
adapter.addSection("section first", new EfficientAdapter(getApplicationContext(),usersPic)); adapter.addSection("section second", new EfficientAdapter(getApplicationContext(),medPic)); listview.setAdapter(adapter);