Some considerations for Android Expandablelistview and ScrollView

Source: Internet
Author: User

There are examples of ScrollView nested ListView before, which is about calculating the height of each item in the ListView. The target effect has been reached. The same Expandablelistview nesting scrollview is also a way of thinking, but to add a little bit more methods and a little bit of change.

To implement this function, first prepare three basic methods:

A method for calculating the height of a Expandablelistview group item (that is, how to calculate the height of a listview merge) Note: Expandablelistview is a subclass of the ListView So, The ListView method It can also be used

/**dynamically change the height of the ListView*/     Public voidSetlistviewheightbasedonchildren (ListView listview) {ListAdapter ListAdapter=Listview.getadapter (); if(ListAdapter = =NULL) {            return; }        intTotalheight = 0;  for(inti = 0; I < Listadapter.getcount (); i++) {View ListItem= Listadapter.getview (I,NULL, ListView); Listitem.measure (0, 0); Totalheight+=listitem.getmeasuredheight (); } viewgroup.layoutparams params=Listview.getlayoutparams (); //params.height = * (Listadapter.getcount ()-1); //params.height = * (Listadapter.getcount ());Params.height =Totalheight+ (Listview.getdividerheight () * (Listadapter.getcount ()-1)); ((marginlayoutparams) params). SetMargins (0, 0, 0, 0);    Listview.setlayoutparams (params); }

Second, the method of calculating the height of the Sub View list under Group Expandablelistview expansion

/*** Extensible ListView Expansion when called * *@paramListView *@paramgroupposition*/     Public Static voidSetexpandedlistviewheightbasedonchildren (Expandablelistview ListView,intgroupposition) {Expandablelistadapter ListAdapter=Listview.getexpandablelistadapter (); if(ListAdapter = =NULL) {            return; } View ListItem= Listadapter.getchildview (groupposition, 0,true,NULL, ListView); Listitem.measure (0, 0); intAppendheight = 0;  for(inti = 0; I < Listadapter.getchildrencount (groupposition); i++) {Appendheight+=listitem.getmeasuredheight (); } viewgroup.layoutparams params=listview.getlayoutparams ();//log.d (TAG, "Expand params.height" + params.height);Params.height + =Appendheight;    Listview.setlayoutparams (params); }

Third, the method of eliminating the height of the sub-view list under Group when calculating Expandablelistview merge

/*** Extensible ListView is called when it is closed * *@paramListView *@paramgroupposition*/     Public Static voidSetcollapselistviewheightbasedonchildren (Expandablelistview ListView,intgroupposition) {Expandablelistadapter ListAdapter=Listview.getexpandablelistadapter (); if(ListAdapter = =NULL) {            return; } View ListItem= Listadapter.getchildview (groupposition, 0,true,NULL, ListView); Listitem.measure (0, 0); intAppendheight = 0;  for(inti = 0; I < Listadapter.getchildrencount (groupposition); i++) {Appendheight+=listitem.getmeasuredheight (); }        /*log.d (TAG, "Collapse childcount=" + listadapter.getchildrencount (groupposition)) ;*/viewgroup.layoutparams params=Listview.getlayoutparams (); Params.height-=Appendheight;    Listview.setlayoutparams (params); }

Three calculation methods are posted, below to see how these three methods are used

First, when setting Expandablelistview's adapter, calculate the width of the merge and show it. (This step is initialized)

For example:

Listview.setadapter (adapter); Listutil.getinstance (). Setlistviewheightbasedonchildren (ListView);

Second, the calculation method is called when the Expandablelistview expands

For example:

/*** Called when Expandablelistview expands*/Listview.setongroupexpandlistener (NewOngroupexpandlistener () {@Override Public voidOngroupexpand (intgroupposition) {                /** LOG.E ("expand", "extended"); for (int i = 0; i < 4; i++) {if * ((groupposition! = i)                 && listview.isgroupexpanded (i)) {* Listview.collapsegroup (i);}} */                /*** Calculate the height of children under Group*/Listutil.setexpandedlistviewheightbasedonchildren (ListView, groupposition); //Update the height of each group itemlistutil.getinstance (). Setlistviewheightbasedonchildren (ListView); }        });

Third, when Expandablelistview merge, call the calculation method

For example:

/*** Called when Expandablelistview shrinks*/Listview.setongroupcollapselistener (NewOngroupcollapselistener () {@Override Public voidOngroupcollapse (intgroupposition) {LOG.E ("Collapse", "contraction"); /** Calculate the height of each subkey under Group, then shrink*/Listutil.setcollapselistviewheightbasedonchildren (ListView, groupposition); /** Re-evaluate group height*/listutil.getinstance (). Setlistviewheightbasedonchildren (ListView); /** Listutil.setcollapselistviewheightbasedonchildren (listView, * groupposition); */            }        });

Well, it's over here. Note: The layout layer in the adapter in the computed party can not be relativelayout, otherwise a null pointer will be reported. For what reason I will not say, a lot of night.

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.