ListView Implementation Grouping

Source: Internet
Author: User

1:fragmenthack4.java

/*** Created by y on 15-1-2.*/ Public classFragmentHack4extendsfragment{View view; List<String>names;    ListView Lvnames;    Namesectionadapter adapter;    TextView Tvheader; intTopvisiblepos; @Override Public voidOnattach (activity activity) {Super. Onattach (activity); Names=NewArraylist<string>(); //test DataString []t={"A", "B", "C", "D", "E", "F"};  for(inti=0;i<t.length;i++){             for(intj=0;j<10;j++) {names.add (T[i]+ "00" +j); } }} @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View= Inflater.inflate (R.layout.fragment_hack4, container,false); Tvheader=(TextView) View.findviewbyid (R.id.tvheader); Lvnames=(ListView) View.findviewbyid (r.id.lvnames); Adapter=NewNamesectionadapter (Getactivity (), r.layout.list_name_item,names);        Lvnames.setadapter (adapter); //set the first Group column dataSettopheader (0);        Lvnames.setonscrolllistener (Lvonscrolllistener ()); returnview; } Abslistview.onscrolllistener Lvonscrolllistener () {return NewAbslistview.onscrolllistener () {@Override Public voidOnscrollstatechanged (Abslistview Abslistview,inti) {} @Override Public voidOnscroll (Abslistview Abslistview,intIintI2,inti3) {                if(i!=Topvisiblepos) {Topvisiblepos=i;                Settopheader (i);    }            }        }; }    Private voidSettopheader (intPOS) {Tvheader.settext (Names.get (POS). SUBSTRING (0,1)); }}

2:namesectionadapter.java

/*** Created by y on 15-1-2.*/ Public classNamesectionadapterextendsArrayadapter<string>{    Private intresourceId;  PublicNamesectionadapter (Context context,intResourceId, list<string>objects) {        Super(context,resourceid,objects);  This. ResourceId =resourceId; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {String name=GetItem (position);        Viewholder Holder; if(convertview==NULL) {Convertview= Layoutinflater.from (GetContext ()). Inflate (ResourceId,NULL); Holder=NewViewholder (); Holder.tvheader=(TextView) Convertview.findviewbyid (R.id.tvheader); Holder.tvname=(TextView) Convertview.findviewbyid (r.id.tvname);        Convertview.settag (holder); }Else{Holder=(Viewholder) Convertview.gettag ();        } holder.tvName.setText (name); //if the first or first letter has changed, the group bar is displayed, otherwise hidden        if(Position==0 | | getItem (position-1). CharAt (0)!=name.charat (0) {holder.tvHeader.setVisibility (view.visible); Holder.tvHeader.setText (Name.substring (0,1)); }Else{holder.tvHeader.setVisibility (view.gone); }        returnConvertview; }    Static classviewholder{ PublicTextView Tvheader;  PublicTextView Tvname; }}

3: Layout file:

(1) Fragment_hack4.xml

<?XML version= "1.0" encoding= "Utf-8"?><Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ListViewAndroid:id= "@+id/lvnames"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"/>    <includeLayout= "@layout/list_header"/></Framelayout>

(2) List_header.xml

<?XML version= "1.0" encoding= "Utf-8"?><TextViewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/tvheader"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "#0000ff"Android:textcolor= "#ffffff"style= "@android: Style/textappearance.large"Android:textstyle= "Bold"/>

(3) List_name_item.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical">    <includeLayout= "@layout/list_header"/>    <TextViewAndroid:id= "@+id/tvname"style= "@android: Style/textappearance.small"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /></LinearLayout>

4: Run Results

ListView Implementation Grouping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.