The effect of grouping is shown in the following table in the Item column:
ListView Grouping Idea: In the Item.xml configuration file, define a display group name of TextView, the TextView only in the current grouping of the first display, the other default does not display (that is, view.gone);
Description of the remaining columns above:
Position column: The position parameter corresponding to the GetView method;
Groupindex: Of course position corresponding item belongs to the first group, or the index number of the current group.
Group_position: The item in the current grouping group name is in the position of the ListView position, such as the position position of Group A in the ListView is 0, and Group B corresponds to the position of 4 in the ListView.
Item.xml similar to the following effect:
Top/Bottom Layout linearlayout<linearlayout> //used to display group names such as a <textview id= "group_name"/>/ / Specific information to display the item such as A1 <textview id= "Item_info"/></linearlayout>
The concrete realization idea and the pseudo code (GetView inside carries on the processing judgment:
1. Determine the current position corresponding item belongs to which or the first group; and gets the index of the group Groupindex
2. According to Groupindex to get the value of group_position, if Group_position==position then let group_name corresponding TextView display otherwise set to View.gone;
To summarize the overall getview of the pseudo-code as follows:
Assume a string[] Group_names = {"A", "B", "C",...} To save the name of the group
Int[] group_positions = {0,4} to hold the current grouped position in the ListView
int groupindex = Getgroupindex (position); int group_position = Getgroup_position (Groupindex); If (group_position== Position) { group_name.setvisiable (true); Group_name.settext (Group_names (Groupindex));} else{group_name.setvisiable (false);}
Gets the pseudo-code for which the current item belongs to a grouping getgroupindex (position) as follows
Gets the current position corresponding Itemitem item = GetItem (position);//Get Groupindexint Groupindex = Arrays.binarysearch (Group_names, Item.getxxx ()) return groupindex>=0? Index:-index-2;item.getxxx () is the uppercase letter that gets the first initial letter of the string displayed in item///Getgroupposition (Groupindex) that gets the current grouping in the corresponding position in the ListView The code is as follows return Group_positions.get (Groupindex)
About the ListView grouping, Android provides the Sectionindexer interface, which provides three methods, the idea of implementing grouping is roughly the same as above, it is not to repeat (spit groove, Personal feeling sectionindexer name than Groupindexer good)
Put your own test code on the following:
Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent "> <listview android:id= "@+id/mylistview" android:layout_width= "fill_parent" android:layout_height= "Fill_ Parent "/></relativelayout>
Item.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" wrap_content " android:o rientation= "Vertical" > <!--group name-- <textview android:id= "@+id/group_name" android: Layout_width= "Fill_parent" android:layout_height= "wrap_content" android:background= "@android: color/ Darker_gray "/> <!--show Information- <textview android:id=" @+id/item_info " android:layout _width= "Wrap_content" android:layout_height= "80sp" android:textsize= "45SP"/></linearlayout>
public class Mainactivity extends Activity {@Override public void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); ListView list = (ListView) Findviewbyid (R.id.mylistview); arraylist<string> stringlist = Initlistdata (); Myadapter adapter = new Myadapter (this, stringlist); List.setadapter (adapter); } private arraylist<string> Initlistdata () {arraylist<string> stringlist = new Arraylist<str Ing> (); Stringlist.add ("Aenegli"); Stringlist.add ("Abddaf"); Stringlist.add ("is"); Stringlist.add ("care"); Stringlist.add ("Ahow"); Stringlist.add ("abide"); Stringlist.add ("Bandidate"); Stringlist.add ("Bapture"); Stringlist.add ("catch"); Stringlist.add ("call"); Stringlist.add ("abuse"); Stringlist.add ("cow"); Row order Collections.sort (stringlist); return stringlist; } }
public class Myadapter extends Baseadapter {//Group name private char groupnames[] = {' A ', ' B ', ' C '}; Private list<integer> grouppositions; Private list<string> Stringarray; Initialize grouppositions private void Initgrouppositions () {grouppositions = new arraylist<integer> (); for (int i = 0; i < stringarray.size (); i++) {if (i==0) {//First Data grouppositions.add (0); continue;} if (i! = Grouppositions.size ()) {String currentstr = Stringarray.get (i); String prestr = Stringarray.get (i-1), char firstcharofcurrentstr = Currentstr.charat (0); char firstcharofprestr = Prestr.charat (0); if (firstcharofcurrentstr!=firstcharofprestr) {grouppositions.add (i);}}} } private context context; Public Myadapter (context context, arraylist<string> arr) {stringarray = arr; This.context = context; Initgrouppositions (); LOG.E ("grouppositions==", grouppositions.size () + ""); } public int GetCount () {return StRingarray.size (); } public String GetItem (int position) {return stringarray.get (position); } public long Getitemid (int position) {return position; Public View GetView (int position, view view, ViewGroup parent) {Layoutinflater inflate = (Activity) context) . Getlayoutinflater (); View = (view) inflate.inflate (R.layout.item, NULL); TextView Groupnameview = (TextView) View.findviewbyid (r.id.group_name); String itemstr = stringarray.get (position); Group processing int groupindex = Getgroupindex (position); int groupposition = getgroupposition (Groupindex); if (position==groupposition) {Groupnameview.settext (groupnames[groupindex]+ ""); Groupnameview.setvisibility (view.visible); }else{groupnameview.setvisibility (View.gone); } TextView TextView = (TextView) View.findviewbyid (r.id.item_info); Textview.settext (ITEMSTR); return view; }//Based on position gets the current item belonging to the first group, that is, the index of the group to get the public int getgroupindex (int position) {String itemstr = GetItem (position); Char Firstchar = Itemstr.touppercase (). charAt (0); int groupindex = Arrays.binarysearch (Groupnames, Firstchar); LOG.E ("Firstchar--groupindex", firstchar+ "---" +groupindex); Return groupindex>=0?groupindex:-groupindex-2; }//According to Groupindex to get the current grouping corresponding to the position public int getgroupposition (int groupindex) {return Grouppositi in the ListView Ons.get (Groupindex); }}
ListView Grouping Implementation Scenario (i)