Nested listview in scrollview in Android

Source: Internet
Author: User

After studying scrollview nesting scrollview, I suddenly want to study the nested listview in scrollview.

If you do not know how scrollview nested scrollview is implemented, refer to the http://www.eoeandroid.com/thread-240709-1-1.html

In the previous articleArticleIn fact, I didn't explain the principle clearly, but I just got down to the originalCodeIn fact, scrollview sets in scrollview and scrollview sets in listview is the same principle.

According to common sense, two problems exist in the scrollview listview:

1. The height of the listview cannot be calculated. Generally, only one row of the listview can be displayed.
2. listview cannot be rolled

When solving problem 1, I found a lot of information on the Internet. How can I make the listview display complete and finally find a post to solve this problem?

Http://blog.csdn.net/hitlion2008/article/details/6737459 (and I accidentally found my "scrollviewNestingScrollview "posts all over the sky... here, I 'd like to ask you to refer to the source of the post .....).

According to this post, I tried it. Although the height of the listview has come out, there are two problems:

A. Only scrollview scrolling is supported. listview does not scroll because the height of listview has reached the maximum and does not need to scroll.
B. the advantage of listview is that it can reuse listitem. If the listview is the largest, It is not reused because listitem is not used. This is the same as listview and does not save UI resources, so it is better for me to directly use linearlayout.

How can we support the scrolling of both the external scrollview and the listview in it?

After thinking about many ways, I finally moved the implementation principle of scrollview sets to scrollview. The result was successful ....

In fact, the implementation principle is very simple. scrollview has a method requestdisallowintercepttouchevent (Boolean );
This method sets whether to hand over the ontouch permission. If the outer scrollview. requestdisallowintercepttouchevent (false); then the outer ontouch permission will be lost, so that the listview can
If you get the ontouch permission, the listview will be able to roll.

The problem is: there is only one permission, and both views must be supported for scrolling. This is a bit difficult to implement ..

In fact, this is not difficult. When the finger reaches the listview, The scrollview outside is handed over the permission. When the finger is released, the scrollview outside is re-authorized. So OK.

See the code implementation:

Override an innerlistview extends listview

Innerlistview. Java

 @ Override  Public   Boolean  Onintercepttouchevent (motionevent eV ){  Switch (EV. getaction ()){  Case  Motionevent. action_down: setparentscrollable (  False ); //  When the finger touches the listview, let the parent scrollview hand over the ontouch permission, that is, let the parent scrollview stop cannot scroll Logmanager. D ("onintercepttouchevent down" );  Case  Motionevent. action_move: logmanager. D ( "Onintercepttouchevent move" );  Break ;  Case  Motionevent. action_up: logmanager. D ( "Onintercepttouchevent up" );  Case  Motionevent. action_cancel: logmanager. D ( "Onintercepttouchevent cancel" ); Setparentscrollable (  True ); //  When the finger is released, the parent scrollview is assigned the ontouch permission again.                  Break  ; Default  :  Break  ;}  Return   Super  . Onintercepttouchevent (EV );}  /**  * Whether to give the rolling event to the parent scrollview **  @ Param  Flag  */      Private   Void Setparentscrollable ( Boolean Flag) {parentscrollview. requestdisallowintercepttouchevent ( ! Flag ); //  The parentscrollview here is the scrollview outside the listview. }

Here I want to propose that the precondition for listview to scroll is that when the height of the listview itself is smaller than that of the subview IN THE listview.

If you set the height of listview to wrap_content in XML, problems may occur. For example, the height of listview is the same as that of the sub-view.
It is best to set the height of the listview. When we write the listview layout, it also gives a fixed value. Such as fill_parent or 100dip

I wrote a parameter named maxheight. Set the maximum height of listview. To ensure that the height of the listview is fixed.

 Private   Int  Maxheight;  Public   Int  Getmaxheight (){ Return  Maxheight ;}  Public   Void Setmaxheight ( Int  Maxheight ){  This . Maxheight = Maxheight ;}@ override  Protected   Void Onmeasure ( Int Widthmeasurespec, Int  Heightmeasurespec ){  // Todo auto-generated method stub          If (Maxheight>-1 ) {Heightmeasurespec = Measurespec. makemeasurespec (maxheight, measurespec. at_most );}  Super  . Onmeasure (widthmeasurespec, heightmeasurespec); system. Out. println (getchildat ( 0 ));} 

 

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.