Android-imitating Expandablelistview with a ListView

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/goldenfish1919/article/details/38334995

Since there are ready-made expandablelistview, why do you have to imitate one ah? Did you take off your pants and fart superfluous? Of course not. Expandablelistview can only support two layers Ah, if you want to support three layers or more layers? or the page structure is more complex, like this:


With the original Expandablelistview is really not very good to achieve, it is not to say no, it seems a little complicated ah. The adapter of the ListView has a Getitemviewtype () and Getviewtypecount () so two methods, very useful, let's try it.

Mainactivity.java:

public class Mainactivity extends Activity {private listviewadapter listviewadapter; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Data list <GroupBean> itemList = Getlistdata ();//view and Adapterlistview listview = (ListView) Findviewbyid (R.id.listview); Listviewadapter = new Listviewadapter (this, itemList);//Set Listener Listviewadapter.setgrouplistener for adapter (new Ongroupclicklistener () {@Overridepublic void ongroupclicked (Groupview view, Groupbean group,int position) { Toast.maketext (Mainactivity.this, "I am" + group.getname (), Toast.length_short). Show ();}); Listviewadapter.setchildlistener (New Onchildclicklistener () {@Overridepublic void onchildclicked (Childview view, Childbean child,int position) {Toast.maketext (mainactivity.this, "I am" + child.getname (), Toast.length_short). Show ();}) ; Listview.setadapter (listviewadapter);} @Overrideprotected void OnDestroy () {if (listviewadapter! = null) {Listviewadapter.desTroy (); listviewadapter = null;} Super.ondestroy ();} private static interface ongroupclicklistener{public void ongroupclicked (Groupview view, Groupbean Group, int position);} private static interface onchildclicklistener{public void onchildclicked (Childview view, Childbean child, int position);}  private static class Listviewadapter extends baseadapter{private static final int view_type_group = 0;private static final int view_type_child = 1;private Context context;private list<groupbean> grouplist;private OnGroupClickListener Grouplistener;private Onchildclicklistener childlistener;public Listviewadapter (context context, List<GroupBean > grouplist) {this.context = Context;this.grouplist = grouplist;} @Overridepublic int GetCount () {if (grouplist = = NULL | | grouplist.size () <= 0) {return 0;} int count = 0;for (Groupbean group:grouplist) {count + +; list<childbean> childlist = Group.getchildlist (); if (childlist = = null) {continue;} if (Group.isexpand ()) {count + = Childlist.size ();}} RetUrn Count;} @Overridepublic Object getItem (int index) {if (grouplist = = NULL | | grouplist.size () <= 0) {return null;} int count = 0; for (int i=0;i<grouplist.size (); i++) {Groupbean group = Grouplist.get (i); if (index = = count) {return group; }count++;if (!group.isexpand ()) {continue;} list<childbean> childlist = Group.getchildlist (); if (childlist = = null) {continue;} for (int j=0;j<childlist.size (); j + +) {Childbean child = Childlist.get (j), if (index = = count) {return child;} count++;}} return null;} @Overridepublic long getitemid (int itemid) {return itemid;} @Overridepublic View GetView (final int position, view Convertview, ViewGroup parent) {Viewholder holder = null;if (CONVERTV Iew = = null) {holder = new viewholder (); int type = Getitemviewtype (position); if (type = = View_type_group) {Convertview = new Groupview (context, parent); Holder.groupview = (Groupview) Convertview;} Else{convertview = new Childview (context, parent); Holder.childview = (Childview) Convertview;} Convertview.settag (holder);} Else{holdeR = (viewholder) Convertview.gettag ();} int type = Getitemviewtype (position), if (type = = View_type_group) {final Groupview Groupview = holder.groupview;final Groupbean Groupbean = (Groupbean) getItem (position); Groupview.setname (Groupbean.getname ()); Groupview.setexpand ( Groupbean.isexpand ()); Groupview.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Groupbean.setexpand (!groupbean.isexpand ()); notifydatasetchanged (); if (grouplistener! = null) { Grouplistener.ongroupclicked (Groupview, Groupbean, Position);}}); return Groupview;} else{final Childview Childview = holder.childview;final Childbean Childbean = (Childbean) getItem (position); Childview.setname (Childbean.getname ()); Childview.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {if (Childlistener! = null) {childlistener.onchildclicked (Childview, Childbean, Position);}}); return Childview;}} @Overridepublic int Getitemviewtype (int position) {Object item = GetItem (position); if (itemInstanceof Groupbean) {return view_type_group;} Else{return View_type_child;}} @Overridepublic int Getviewtypecount () {return 2;} public void Setgrouplistener (Ongroupclicklistener grouplistener) {this.grouplistener = Grouplistener;} public void Setchildlistener (Onchildclicklistener childlistener) {this.childlistener = Childlistener;} public void Destroy () {this.context = Null;this.grouplist = Null;this.grouplistener = Null;this.childlistener = null;} Private class Viewholder{groupview Groupview; Childview Childview;}} Private list<groupbean> Getlistdata () {list<groupbean> itemList = new arraylist<groupbean> (); for ( int i=0;i<10;i++) {Groupbean group = new Groupbean (); Group.setname ("Group_" + (i+1)); list<childbean> childlist = new arraylist<childbean> (); for (int. j=0;j<3;j++) {Childbean child = new Childbean (); Child.setname ("Child" +j), Childlist.add (child); Group.setchildlist (childlist); Itemlist.add (group);} return itemList;} private Static Class Groupbean{private BooleanIsexpand;private string name;private list<childbean> childlist;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public list<childbean> getchildlist () {return childlist;} public void Setchildlist (list<childbean> childlist) {this.childlist = childlist;} public Boolean Isexpand () {return isexpand;} public void Setexpand (Boolean isexpand) {This.isexpand = Isexpand;}} private static Class Childbean{private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;}} private static class Groupview extends Relativelayout {private TextView nameview;private ImageView arrowview;public Group View (Context context, ViewGroup parent) {super (context); init (parent);} private void Init (ViewGroup parent) {final Layoutinflater mlayoutinflater = Layoutinflater.from (GetContext ()); View v = mlayoutinflater.inflate (R.layout.list_item_group, parent, false); AddView (v); Nameview = (TextView) V.findviewbyid (R.id.username);Arrowview = (ImageView) V.findviewbyid (R.id.group_arrow);} public void SetName (String name) {this.nameView.setText (name);} public void Setexpand (Boolean expand) {if (expand) {Arrowview.setbackgroundresource (r.drawable.arrow_down);} Else{arrowview.setbackgroundresource (R.drawable.arrow_right);}}} private static class Childview extends Relativelayout {private TextView nameview;public Childview (context context, VIEWGR OUP parent) {super (context); init (parent);} private void Init (ViewGroup parent) {final Layoutinflater mlayoutinflater = Layoutinflater.from (GetContext ()); View v = mlayoutinflater.inflate (R.layout.list_item_child, parent, false); AddView (v); Nameview = (TextView) V.findviewbyid (r.id.username);} public void SetName (String name) {this.nameView.setText (name);}}}
Activity_main.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical" >    <listview        android:id= "@+id/listview"        android:layout_ Width= "Match_parent"        android:layout_height= "Match_parent"/></linearlayout>
List_item_group.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Wrap_content " >    <imageview        android:id= "@+id/group_arrow"        android:layout_width= "Wrap_content"        android: layout_height= "Wrap_content"        android:layout_alignparentleft= "true"        android:layout_centervertical= "true "        android:layout_marginleft=" 20DP "/>    <textview        android:id=" @+id/username "        android: Layout_width= "Wrap_content"        android:layout_height= "50DP"        android:layout_centerinparent= "true"/> </RelativeLayout>
List_item_child.xml:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Wrap_content " >    <imageview        android:id= "@+id/usericon"        android:layout_width= "50DP"        android:layout_ height= "50DP"        android:src= "@drawable/ic_launcher"/>    <textview        android:id= "@+id/username"        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:layout_ marginleft= "20DP"        android:layout_torightof= "@id/usericon"/></relativelayout>
SOURCE in: http://download.csdn.net/detail/goldenfish1919/7703143

Related Article

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.