Android expandablelistview with Group indicator

Source: Internet
Author: User

We all knowThe default expandablelistview group header of Android cannot be fixed on the interface. When scrolling down, it cannot indicate the group to which the currently displayed child belongs,I searched the internet for a lot of expandablelistviews about the effect of QQ friend groups on Imitation mobile phones, and found that they were not satisfactory. As a result, I improved a little bit on the basis of others. In fact, the principle is similar, it only adds the animation effects to be squeezed out, and it is simpler, but it still does not completely achieve the same effect as QQ. I hope some experts can achieve more realistic effects. Let's take a look at it first:

I have not set expandablelistview independently to form a new control. Like many friends on the Internet, I listen to the onscrolllistener event. When the group is not in the first position, display the indicator in our header and make its view the same as the view in the group where the current child is located. Then add an event to close the group, that is, the effect of a simple QQ Group of Friends is achieved.

Next let's take a look at the main layout file: Main. xml. The following topgroup's framelayout is our indicator.

<? XML version = "1.0" encoding = "UTF-8"?> <Framelayout xmlns: Android = "" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <expandablelistview Android: Id = "@ + ID/expandablelistview" android: layout_width = "match_parent" Android: layout_height = "match_parent"> </strong> <framelayout Android: Id = "@ + ID/topgroup" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: Orientation = "vertical"> </framelayout>

The most important part is the following: we listen to the onsroll interface event and process the listview as it slides,CodeThere are many comments in, so I will not describe them here.

/*** Here is very importance for indicator Group */Public void onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {final expandablelistview listview = (batch) view; /*** calculate point (0, 0) */INT NPOs = view. pointtoposition (0, 0); // It is actually firstvisibleitemif (NPOs = adapterview. invalid_position) // return if the value of the first position is invalid; long Pos = listview. getexpandabl Elistposition (NPOs); int childpos = expandablelistview. getpackedpositionchild (POS); // obtain the idint grouppos = expandablelistview of the first row of child. getpackedpositiongroup (POS); // obtain the idif (childpos = adapterview. invalid_position) {// The first line does not show child or group, so there is no need to display the indicator view groupview = listview. getchildat (NPOs-listview. getfirstvisibleposition (); // viewindicatorgroupheight = groupview in the first row. getheight (); // get gro Indicatorgroup. setvisibility (view. gone); // hide indicator} else {indicatorgroup. setvisibility (view. visible); // if the first row is child, the indicator is displayed.} // get an error data, so return nowif (indicatorgroupheight = 0) {return ;} // update the data of indicator group viewif (grouppos! = Indicatorgroupid) {// if the indicator does not display the current groupmadapter. getgroupview (grouppos, listview. isgroupexpanded (grouppos), indicatorgroup. getchildat (0), null); // update the indicator to the current groupindicatorgroupid = grouppos; log. E (TAG, pre + "bind to new group, group position =" + grouppos); // madapter. hidegroup (indicatorgroupid); // we set this group view // to be hided // Add the Click Event indicatorgroup for this indicator. setonclicklistener (New onclicklisten Er () {public void onclick (view v) {// todo auto-generated method stublistview. collapsegroup (indicatorgroupid) ;}}) ;}if (indicatorgroupid =-1) // If the Grop ID is invalid at this time, return;/*** calculate point (0, indicatorgroupheight) The following figure shows the push-up effect */INT showheight = indicatorgroupheight; int nendpos = listview. pointtoposition (0, indicatorgroupheight); // The position of the second item if (nendpos = adapterview. invalid_position) // directly if it is invalid Return return; long pos2 = listview. getexpandablelistposition (nendpos); int grouppos2 = expandablelistview. getpackedpositiongroup (pos2); // obtain the idif (grouppos2! = Indicatorgroupid) {// if it is not equal to the current groupview viewnext = listview of the indicator. getchildat (nendpos-listview. getfirstvisibleposition (); showheight = viewnext. gettop (); log. E (TAG, pre + "update the show part height of indicator group:" + showheight);} // Update group positionmarginlayoutparams layoutparams = (marginlayoutparams) indicatorgroup. getlayoutparams (); layoutparams. topmargin =-(indicatorgroupheight-showheight); indicatorgroup. setlayoutparams (layoutparams );}

Source code:Http://

Finally, I would like to share with you another code that imitates the iPhone Address Book effect. It is an extension of listview, with the best effect. We hope that experts can extend expandablelistview to the same effect.Http://

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: 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.