Android ScrollView Nesting listview

Source: Internet
Author: User

ScrollView set of ListView will save the following two questions

1. The listview height in the inside cannot be counted, usually only one row of the ListView is displayed

2.listview cannot scroll

I'm using the expandable one here (Expandablelistview

Problem one, if Expandablelistview is a child that is loaded with a dead layout file, simply call this static method after setting the adapter of the ListView to make the ListView appear correctly in the ListItem of its parent listview. Note, however, that each item of the child ListView must be linearlayout, not the other, because other layout (such as relativelayout) does not rewrite onmeasure (), so it will be in the Onmeasure () Throws an exception.

1  Public Static voidSetlistviewheightbasedonchildren (ListView listview) {2ListAdapter ListAdapter =Listview.getadapter (); 3         if(ListAdapter = =NULL) { 4             return; 5         }   6         intTotalheight = 0; 7          for(inti = 0; I < Listadapter.getcount (); i++) { 8View ListItem = Listadapter.getview (i,NULL, ListView); 9Listitem.measure (0, 0); TenTotalheight + =listitem.getmeasuredheight (); One         }    AViewgroup.layoutparams params =listview.getlayoutparams (); -Params.height = Totalheight + (listview.getdividerheight () * (Listadapter.getcount ()-1));  - listview.setlayoutparams (params); the}

If the Expandablelistview subkey is dynamically loaded with code, define a height for each subkey, which will calculate the height of the entire listview. The code is as follows:

Define the height of the child first

public static final int itemheight = 85;

Define the TextView layout of a subkey

1 Static  PublicTextView Gettextview (context context) {2Abslistview.layoutparams LP =NewAbslistview.layoutparams (3 ViewGroup.LayoutParams.FILL_PARENT, itemheight);4TextView TextView =NewTextView (context);5 TEXTVIEW.SETLAYOUTPARAMS (LP);6 Textview.settextcolor (color.black);7Textview.setgravity (gravity.center_vertical |gravity.left);8       returnTextView;9}

The following method is then overridden in extends baseexpandablelistadapter , where Getchild (Groupposition, childposition). ToString () Is the text that is read from the array to be displayed in the subkey TextView.

1 @Override2     PublicView Getchildview (intGroupposition,intChildposition,3         BooleanIslastchild, View Convertview, ViewGroup parent) {4TextView TextView = Gettextview ( This. Parentcontext);5 Textview.settext (Getchild (Groupposition, childposition). toString ());6Textview.setpadding (mypaddingleft + paddingleft, 0, 0, 0);7       returnTextView;8}

In this way, the problem is solved.

The second problem is that the ListView cannot slide after the ScrollView is nested in the ListView, and can only see part of the ListView that is displayed. The solution principle is simple scrollview there is a method Requestdisallowintercepttouchevent (Boolean), this method is to set whether to surrender Ontouch permissions, If you let the outer layer of scrollview.requestdisallowintercepttouchevent (false), then the outer ontouch permission will be lost, so that the inside of the ListView can get Ontouch permission, the ListView can scroll.

The problem is that there is only one permission, and you can scroll to support two view. Monitor Ontouch's motionevent. Action_scroll: event when the finger touches the ListView, let the outside ScrollView surrender the permission, and when the finger touches the outside of the ListView, let the outside ScrollView regain access. That's OK. R.id. hlpg_main_lv: Is the listview corresponding to the id,r.id. hlpg_main_allcontent: Is the ID of the layout outside the ListView.

And look at the code implementation:

1      CaseR.ID.HLPG_MAIN_LV:2Sview.requestdisallowintercepttouchevent (false);3             Break;4       Caser.id.hlpg_main_allcontent:5Sview.requestdisallowintercepttouchevent (true);6             Break;7      default:               8Sview.requestdisallowintercepttouchevent (true);9             Break;Ten                      

Then the second problem is solved.

The ListView also provides monitoring of the ListView scrolling, which can be set up to do the appropriate actions.

1Expandablelistview.setonscrolllistener (NewOnscrolllistener () {2 @Override3          Public voidOnscrollstatechanged (Abslistview view,intscrollstate) {4            Switch(scrollstate) {//When you do not scroll5             CaseOnscrolllistener.scroll_state_idle:6               //TODO coding here dosometing7                Break;8            }9         }      Ten @Override One          Public voidOnscroll (Abslistview view,intFirstvisibleitem, A               intVisibleItemCount,intTotalitemcount) { -             //TODO coding here dosometing -         } the});

This problem is encountered in the development process, initially thinking in other ways to achieve, found that more and more complex is not up to the desired effect, and finally read the data to find that can be switched between ScrollView and ListView Ontouch focus to achieve nested sliding, I hope this technology will be shared to solve the same problems we encountered.

Android ScrollView Nesting listview

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.