Android with the group indicator Expandablelistview (self-write) _android

Source: Internet
Author: User
Tags first row
We all know that the Android default Expandablelistview group header cannot be pinned to the interface, and when scrolling down, it is not possible to indicate to which group the child is currently displayed. Search on the Internet a lot about imitation mobile QQ Friends Group effect of the Expandablelistview, found that are not the best, and then on the basis of others improved a little bit, in fact, the principle is similar, just increased to squeeze out the animation effect, and more simple, Just still did not fully reach the same effect as QQ, I hope that a master can achieve more realistic effect, the following we look at the effect of the picture:

I do not have the Expandablelistview independent to form a new control, like many friends on the Internet, listening to Onscrolllistener events, when the group is not in the first position, the head of the indicator show us, And make it view the same as the current child's group view, and then add a click to close the group of events, that is, to achieve a simple imitation QQ friends group effect.
Let's take a look at the main layout files: Main.xml, the Topgroup framelayout below is our indicator.
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<framelayout xmlns:android= "Http://schemas.android.com/apk/res/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" >
</ExpandableListView>
<framelayout
Android:id= "@+id/topgroup"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical" >
</FrameLayout>
</FrameLayout>

The most important is the following section: We listen to onsroll this interface event, when the ListView in the sliding, do the corresponding processing, the code comments more, I do not have more to explain here.
Copy Code code as follows:

/**
* We are very importance for indicator group
*/
public void Onscroll (Abslistview view, int firstvisibleitem,
int visibleitemcount, int totalitemcount) {
Final Expandablelistview ListView = (expandablelistview) view;
/**
* Calculate Point (0,0)
*/
int NPOs = view.pointtoposition (0, 0);/it's actually firstvisibleitem.
if (NPOs = = adapterview.invalid_position)//If the first position value is not valid
Return
Long pos = listview.getexpandablelistposition (NPOs);
int childpos = Expandablelistview.getpackedpositionchild (POS);//Get ID of first line child
int grouppos = Expandablelistview.getpackedpositiongroup (POS);//Get ID of First row group
if (Childpos = = adapterview.invalid_position) {//The first row is not a child, or group, there is no need to display the indicator at this time
View Groupview = Listview.getchildat (NPOs
-Listview.getfirstvisibleposition ());//view of the first line
Indicatorgroupheight = Groupview.getheight ();//Get the height of the group
Indicatorgroup.setvisibility (View.gone);//Hide Indicator
} else {
Indicatorgroup.setvisibility (view.visible);//scroll to the first row is child, show indicator
}
Get a error data
if (Indicatorgroupheight = = 0) {
Return
}
Update the data of indicator Group view
if (Grouppos!= indicatorgroupid) {//If the indicator does not display the current group
Madapter.getgroupview (Grouppos, listview.isgroupexpanded (Grouppos),
Indicatorgroup.getchildat (0), NULL)//update indicator to current group
Indicatorgroupid = Grouppos;
LOG.E (TAG, PRE + "bind to new Group,group position =" + Grouppos);
Madapter.hidegroup (INDICATORGROUPID); We set this group view
To be hided
Increase the Click event for this indicator
Indicatorgroup.setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {
TODO auto-generated Method Stub
Listview.collapsegroup (INDICATORGROUPID);
}
});
}
if (indicatorgroupid = = 1)//If the ID of the Grop is not valid at this time, return
Return
/**
* Calculate point (0,indicatorgroupheight) below is the effect of forming upwards
*/
int showheight = Indicatorgroupheight;
int nendpos = listview.pointtoposition (0, indicatorgroupheight);//position of second item
if (Nendpos = = adapterview.invalid_position)//If invalid direct return
Return
Long Pos2 = Listview.getexpandablelistposition (Nendpos);
int groupPos2 = Expandablelistview.getpackedpositiongroup (POS2);//Get ID of second group
if (GroupPos2!= indicatorgroupid) {//if not equal to indicator current group
View Viewnext = Listview.getchildat (nendpos
-Listview.getfirstvisibleposition ());
Showheight = Viewnext.gettop ();
LOG.E (TAG, PRE + "Update" the show part height of indicator group: "
+ showheight);
}
Update group position
Marginlayoutparams layoutparams = (marginlayoutparams) indicatorgroup
. Getlayoutparams ();
Layoutparams.topmargin =-(indicatorgroupheight-showheight);
Indicatorgroup.setlayoutparams (Layoutparams);
}

This article source code downloads
Finally, let's share another code that mimics the iphone address Book effect. It is the extension of the ListView, the best effect. Hope that a master can extend the Expandablelistview to the same effect.
SOURCE download
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.