Sometimes we want to classify on listView, or sometimes we want to display two columns in one row. Sometimes we need three columns of content. How can we achieve this? Here we need to use
Java code
Class Section {
String caption;
Adapter adapter;
Section (String caption, Adapter adapter ){
This. caption = caption;
This. adapter = adapter;
}
}
You can customize a class. This class can contain multiple adapters. You can use this class if you want to use it.
Java code
Abstract public class SectionedAdapter extends BaseAdapter {
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 ();
}
Public void addSection (String caption, Adapter adapter ){
Sections. add (new Section (caption, adapter ));
}
Public Object getItem (int position ){
For (Section 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 Section: this. sections ){
Total + = section. adapter. getCount () + 1; // add one for header
}
Return (total );
}
Public int getViewTypeCount (){
Int total = 1; // one for the header, plus those from sections
For (Section section Section: this. sections ){
Total + = section. adapter. getViewTypeCount ();
}
Return (total );
}
Public int getItemViewType (int position ){
Int typeOffset = TYPE_SECTION_HEADER + 1; // start counting from here
For (Section section Section: this. sections ){
If (position = 0 ){
Return (TYPE_SECTION_HEADER );
}
Int size = section. adapter. getCount () + 1;
If (position <size ){
Return (typeOffset + section. adapter. getItemViewType (position-1 ));
}
Position-= size;
TypeOffset + = section. adapter. getViewTypeCount ();
}
Return (-1 );
}
Public boolean areAllItemsSelectable (){
Return (false );
}
Public boolean isEnabled (int position ){
Return (getItemViewType (position )! = TYPE_SECTION_HEADER );
}
@ Override
Public View getView (int position, View convertView,
ViewGroup parent ){
Int sectionIndex = 0;
For (Section section Section: this. sections ){
If (position = 0 ){
Return (getHeaderView (section. caption, sectionIndex, convertView, parent ));
}
Int size = section. adapter. getCount () + 1;
If (position <size ){
Return (section. adapter. getView (position-1, convertView, parent ));
}
Position-= size;
SectionIndex ++;
}
Return (null );
}
@ Override
Public long getItemId (int position ){
Return (position );
}
Class Section {
String caption;
Adapter adapter;
Section (String caption, Adapter adapter ){
This. caption = caption;
This. adapter = adapter;
}
}
}
Then the main class is
Java code
Public class SectionedDemo extends ListActivity {
Private static String [] items = {"lorem", "ipsum", "dolor", "purus "};
@ Override
Public void onCreate (Bundle icicle ){
Super. onCreate (icicle );
SetContentView (R. layout. main );
Adapter. addSection ("Original", new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1,
Items ));
List <String> list = Arrays. asList (items );
Collections. shuffle (list );
Adapter. addSection ("Shuffled", new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1,
List ));
List = Arrays. asList (items );
Collections. shuffle (list );
Adapter. addSection ("Re-shuffled", new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1,
List ));
SetListAdapter (adapter );
}
SectionedAdapter adapter = new SectionedAdapter (){
Protected View getHeaderView (String caption, int index, View convertView, ViewGroup parent ){
TextView result = (TextView) convertView;
If (convertView = null ){
Result = (TextView) getLayoutInflater (). inflate (R. layout. header, null );
}
Result. setText (caption );
Return (result );
}
};
}
Other things need to be changed by yourself. I just want to disrupt the content here.
There are some things that I only provide for simple mediation without the source code or I don't think it is very simple.
For complex or hard-to-be-clear expressions, the source code will be sent.
Author: "zhujianjia"