A bug analysis under the use of android:scrollview+swiperefreshlayout combination

Source: Internet
Author: User
<span id="Label3"></p><p><p>Today the group of colleagues to do a wonderful effect, asked in ScrollView nested a Refreshlayout. The code looks like This:</p></p><pre><?xml version= "1.0" encoding= "utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">android:orientation</span></span>= "vertical" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Red Background</span></span><<span style="color: #000000;"><span style="color: #000000;">ScrollView android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#ff00ff" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Yellow background</span></span><<span style="color: #000000;"><span style="color: #000000;">android.support.v4.widget.SwipeRefreshLayout android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "fill_parent"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#ffff00" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Black Background</span></span><<span style="color: #000000;"><span style="color: #000000;">LinearLayout android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#000000"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "100dp"/> </android.support.v4.widget.SwipeRefreshLayout> </scrollview></linearlayout&gt ;</pre><p><p>The expected effect is this:</p></p><p><p>(the Blue part is toolsbar, please ignore)</p></p><p><p>and the actual effect is this:</p></p><p><p></p></p><p><p>Very strange, obviously set the height of the swiperefreshlayout is fill_parent, why not show at all? To know, in the Swiperefreshlayout also set a height of 100dp linearlayout, normally swiperefreshlayout at least also occupy the height of 100dp ah, Now the height incredibly is 0.</p></p><p><p>This question scored in two parts stating:</p></p><p><p>Height Measurement of 1.ScrollView.</p></p><p><p>Height Measurement of 2.SwipeRefreshLayout.</p></p><p><p>Before this, a brief introduction to the view measurement of the three modes (mode), the specific view of the measurement process is not detailed, you can find some information on the Internet.</p></p><p><p></p></p><p><p>At_most: maximum size mode, typically set to wrap_content, is measured using this mode, and the child view does not exceed the maximum width of the parent view.</p></p><p><p>Exactly: precise mode, typically set to fill_parent, will use this measurement mode, the parent view to how much width, the child view to use how Much.</p></p><p><p>UNSPECIFIED: no pattern is specified, the child view is measured how big it is, and is not affected by the width and height of the parent view.</p></p><p><p></p></p><p><p>The height measurement of ScrollView is found in this article for reasons and solutions (foreigner, needs fq). The focus is on the Fillviewport Property. Let's first replace the swiperefreshlayout in the layout file with the Linearlayout. Look at the effect</p></p><pre><?xml version= "1.0" encoding= "utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">android:orientation</span></span>= "vertical" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Red Background</span></span><<span style="color: #000000;"><span style="color: #000000;">ScrollView android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#ff00ff" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Yellow background</span></span><<span style="color: #000000;"><span style="color: #000000;">LinearLayout android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "fill_parent"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#ffff00" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Black Background</span></span><<span style="color: #000000;"><span style="color: #000000;">LinearLayout android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "100dp"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#000000"/> </LinearLayout> </ScrollView></LinearLayout></pre><p><p></p></p><p><p>We will find that although the ScrollView internal LinearLayout has been set to fill_parent, its height is still only 100dp. next, set the Fillviewport of ScrollView to True.</p></p><pre><?xml version= "1.0" encoding= "utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">android:orientation</span></span>= "vertical" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Red Background</span></span><<span style="color: #000000;"><span style="color: #000000;">ScrollView android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:fillviewport</span></span>= "true"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#ff00ff" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Yellow background</span></span><<span style="color: #000000;"><span style="color: #000000;">LinearLayout android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "fill_parent"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#ffff00" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Black Background</span></span><<span style="color: #000000;"><span style="color: #000000;">LinearLayout android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "100dp"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#000000"/> </LinearLayout> </ScrollView></LinearLayout></pre><p><p></p></p><p><p>normal, LinearLayout filled with the height of the SCROLLVIEW.</p></p><p><p>Then take a look at some of the ScrollView and measurement-related methods:</p></p><pre><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onmeasure (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>widthmeasurespec,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">HEIGHTMEASURESPEC) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. Onmeasure (widthmeasurespec, heightmeasurespec); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">mfillviewport) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Heightmode =<span style="color: #000000;"><span style="color: #000000;">Measurespec.getmode (heightmeasurespec); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(heightmode = =<span style="color: #000000;"><span style="color: #000000;">Measurespec.unspecified) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(getchildcount () > 0<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>View child = Getchildat (0<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Height =<span style="color: #000000;"><span style="color: #000000;">Getmeasuredheight (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(child.getmeasuredheight () <<span style="color: #000000;"><span style="color: #000000;">Height) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Framelayout.layoutparams LP =<span style="color: #000000;"><span style="color: #000000;">(layoutparams) Child.getlayoutparams (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Childwidthmeasurespec =<span style="color: #000000;"><span style="color: #000000;">getchildmeasurespec (widthmeasurespec, mpaddingleft</span></span>+<span style="color: #000000;"><span style="color: #000000;">mpaddingright, lp.width); Height</span></span>-=<span style="color: #000000;"><span style="color: #000000;">mpaddingtop; Height</span></span>-=<span style="color: #000000;"><span style="color: #000000;">mpaddingbottom; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Childheightmeasurespec =<span style="color: #000000;"><span style="color: #000000;">Measurespec.makemeasurespec (height, measurespec.exactly); Child.measure (childwidthmeasurespec, childheightmeasurespec); }</span> }} @Override</span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Measurechild (View child,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>parentwidthmeasurespec,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">PARENTHEIGHTMEASURESPEC) {viewgroup.layoutparams LP</span></span>=<span style="color: #000000;"><span style="color: #000000;">Child.getlayoutparams (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">childwidthmeasurespec; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">childheightmeasurespec; Childwidthmeasurespec</span></span>=<span style="color: #000000;"><span style="color: #000000;">getchildmeasurespec (parentwidthmeasurespec, mpaddingleft</span></span>+<span style="color: #000000;"><span style="color: #000000;">mpaddingright, lp.width); Childheightmeasurespec</span></span>= Measurespec.makemeasurespec (0<span style="color: #000000;"><span style="color: #000000;">, measurespec.unspecified); Child.measure (childwidthmeasurespec, childheightmeasurespec); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Measurechildwithmargins (View child,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>parentwidthmeasurespec,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">widthused,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>parentheightmeasurespec,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Heightused) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Marginlayoutparams LP =<span style="color: #000000;"><span style="color: #000000;">(marginlayoutparams) Child.getlayoutparams (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Childwidthmeasurespec =<span style="color: #000000;"><span style="color: #000000;">getchildmeasurespec (parentwidthmeasurespec, mpaddingleft</span></span>+ mpaddingright + Lp.leftmargin +<span style="color: #000000;"><span style="color: #000000;">Lp.rightmargin</span></span>+<span style="color: #000000;"><span style="color: #000000;">widthused, lp.width); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Childheightmeasurespec =<span style="color: #000000;"><span style="color: #000000;">Measurespec.makemeasurespec (lp.topmargin</span></span>+<span style="color: #000000;"><span style="color: #000000;">lp.bottommargin, measurespec.unspecified); Child.measure (childwidthmeasurespec, childheightmeasurespec); }</span></span></pre><p><p></p></p><p><p><span><span style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5; background-color: #ffffff;">See the source code will know, ScrollView inherit from framelayout, so super.onmeasure (widthmeasurespec, heightmeasurespec); is to perform the framelayout height measurement method. But here you rewrite the</span></span> measurechildwithmargins method, which in this step sets the assignable height to Measurespec.makemeasurespec (lp.topmargin + lp.bottommargin , measurespec.unspecified), which causes the fill_parent of the child view to be invalid. (this step does not know how to look at the principle of view measurement and several modes of measurespec).</p></p><p><p><span><span style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5; background-color: #ffffff;">So after super.onmeasure, the sub view can only get to its own height (the child view of the child view of the maximum height), but if the Fillviewport set to True,scrollview will also call another step of measurement:</span></span></p></p><p><p>1. Get the first child view, which is the linearlayout in the XML file above</p></p><p><p>2. Get ScrollView height, because the ScrollView height is set to fill_parent, so it is the screen height.</p></p><p><p>2. Obtain the height of the sub-view measured (before the super.onmeasure measurement), if the sub-view height is less than the ScrollView height, will be a second measurement, the parameters of this measurement is this:<em id="__mceDel" style="font-family: ‘Courier New‘; font-size: 12px; line-height: 1.5;">int Childheightmeasurespec =</em></p></p><pre><pre><em id="__mceDel"><span> Measurespec.makemeasurespec (height, measurespec.exactly);<br></span></em></pre></pre><p><p>That is, Sub-view can be assigned a height of ScrollView itself.</p></p><p><p></p></p><p><p>This way, when Scrollview's child view is linearlayout, as long as the set Fillviewort is true, we can achieve the desired effect, but instead of Swiperefreshlayout see</p></p><pre><?xml version= "1.0" encoding= "utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">android:orientation</span></span>= "vertical" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Red Background</span></span><<span style="color: #000000;"><span style="color: #000000;">ScrollView android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:fillviewport</span></span>= "true"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#ff00ff" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Yellow background</span></span><<span style="color: #000000;"><span style="color: #000000;">android.support.v4.widget.SwipeRefreshLayout android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "fill_parent"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#ffff00" ><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Black Background</span></span><<span style="color: #000000;"><span style="color: #000000;">LinearLayout android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "100dp"<span style="color: #000000;"><span style="color: #000000;">Android:background</span></span>= "#000000"/> </android.support.v4.widget.SwipeRefreshLayout> </ScrollView></LinearLayout></pre><p><p></p></p><p><p></p></p><p><p>It's all black! That means that the swiperefreshlayout internal linearlayout into fill_parent, but its height is fixed 100dp ah.</p></p><p><p>All right, keep looking at Swiperefreshlayout's onmeasure method:</p></p><pre><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onmeasure (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>widthmeasurespec,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">HEIGHTMEASURESPEC) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. Onmeasure (widthmeasurespec, heightmeasurespec); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mtarget = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {ensuretarget (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mtarget = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">; } mtarget.measure (measurespec.makemeasurespec (getmeasuredwidth ()</span></span>-getpaddingleft ()-<span style="color: #000000;"><span style="color: #000000;">getpaddingright (), measurespec.exactly), measurespec.makemeasurespec (getmeasuredheight () </span></span>-getpaddingtop ()-<span style="color: #000000;"><span style="color: #000000;">Getpaddingbottom (), measurespec.exactly)); Mcircleview.measure (measurespec.makemeasurespec (mcirclewidth, measurespec.exactly), MeasureSpec.makeMeasure Spec (mcircleheight, measurespec.exactly)); .... }</span></span></pre><p><p>The mtarget here is Swiperefreshlayout's first child view, the linearlayout in our XML Above. We can see that the incoming height here is</p></p><pre><pre><span>Measurespec.makemeasurespec ( getmeasuredheight ()-getpaddingtop ()-<span> getpaddingbottom (), Measurespec.exactly)</span></span></pre></pre><p><p><span>It is clear that the child view is forced to be set to the height of the Swiperefreshlayout.</span></p></p><p><p><span>Here are all the issues solved, a summary:</span></p></p><p><p><span>The measurement method of the 1.ScrollView internal view is set to unspecified by default, in which case the child view is displayed according to its actual height, so setting the fill_parent is not valid.<br></span></p></p><p><p><span>2. Set the Fillviewport property of ScrollView to true, in which case the height measurement is re-measured when the child view height is less than the parent view height, and the child view height is forcibly set to the parent view Height. When the child view height is greater than the parent view height, it is Unaffected. That is not full screen will forcibly become force full screen, The original full screen can continue to Scroll.</span></p></p><p><p><span>The 3.SwipeRefreshLayout height measurement method will force the child view to be set to its own height.</span></p></p><p><p></p></p><p><p><span>finally, Scrollview+swiperefreshlayout's nesting combination is a stupid way ... After we have solved the above problem, we simply abandon it. Only when learning new Knowledge.</span></p></p><p><p>A bug analysis under the use of android:scrollview+swiperefreshlayout combination</p></p></span>

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.