Android does not mess with view.getheight () (especially UI line thread)

Source: Internet
Author: User

Reason: This is a very interesting bug view.getheight (), get the height incredibly and we think not the same, this is a bug from the Xlistview, just started thinking is Scroller did not execute, after one hours of debugging, found that the reason is here, View.getheight (), the height returned is not the same as the true height. Solution: The original code
public int getvisiableheight () {return mcontainer.getheight ();}
After the fixed code:
public int getvisiableheight () {return mcontainer.getlayoutparams (). Height;}

Why is it? Let's take a look at the log.
02-12 18:15:49.774:e/windows (5761): Mcontainer.getheight (): 15702-12 18:15:49.774:e/windows (5761): Mcontainer.getlayoutparams (). height:28402-12 18:15:49.774:e/windows (5761): Mcontainer.getheight (): 15702-12 18:15:49.774:e/windows (5761): Mcontainer.getlayoutparams (). height:28402-12 18:15:49.774:e/windows (5761): Mcontainer.getheight (): 15702-12 18:15:49.774:e/windows (5761): Mcontainer.getlayoutparams (). height:28402-12 18:15:49.884:e/windows (5761): Mcontainer.getheight (): 24702-12 18:15:49.884:e/windows (5761): Mcontainer.getlayoutparams (). height:240

As you can see, there are two big gaps, why? This is mainly because of the computational problem. Mcontainer.getlayoutparams (). Height is a fixed value that you set relative to the parent control. and Mcontainer.getheight () The source code is written like this
  Public final int getheight () {        return mbottom-mtop;    }
The bottom minus the top, it can be calculated, but this process is unreliable, because of the multithreading problem, when we handle Ontouchevent UI thread has not been refreshed, UI thread and ontouchevent is the same thread, do not believe themselves blocked under try to know, The View.getheight () we get is definitely the wrong data, and Getlayoutparams (). Height is a fixed value, so it must be the value when the view is refreshed.



Android does not mess with view.getheight () (especially UI line thread)

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.