Resolve Listview,gridview GetView Multiple call issues

Source: Internet
Author: User

  • In development we often encounter some wonderful problems, but we still have to find a way to solve the AH, and then the online solution is indeed more, but we still have to solve Ah, and then we must first Test.

    These 2 controls load the custom content when you want to load 2 times, but when you print, you find that he runs more than 2 times, GetView and GetCount log is called wildly, and the log in GetView is the total number of bars each time, and the position value of this total number of data is 0. The GetView of a GridView will be called repeatedly. Especially when the itemview need to load pictures, it is easy to cause too much GC, it is easy to appear ANR, then because of what?

    2.-Non-custom controls (native)
    This is a number of calls that occur when the Android mechanism is high, so if you are not a custom control, you might want to change the width and height of the control to: this is changed to a property: Android:layout_width= "Fill_parent" Android:layout_height= "Fill_parent, the best of its parent layout is also set to such a good.

  • custom controls (need to override Onmeasure and OnLayout)
    These controls are custom, often the layout of their item is complex, and then we need to dynamically calculate the height of the control, and then where the layout is to be calculated, It may be misplaced if you make a random grab.
    Google explains that the view is divided into two stages in draw: measure and layout, in the measure phase, mainly to calculate two parameters: height and width. It is also important to note that this is a recursive process, from top to bottom, and Decorview begins to invoke the measure of its own child elements in turn. After calculating the two parameters, the layout is started, and then the call of draw is finished. For a ListView, of course each item is called the measure method, and in this process GetView and GetCount are called.
    then our display interface will typically use multiple invocations of Baseadapter,getview (), so our reuse of Converview is critical. (Judging whether in onmeasure, if, then just Minflater.inflate (r.layout.xxx), then immediately return to this convertview, if not in Onmeasure, Then go to the real onlayout) here is the reference [http://blog.csdn.net/ksr12333/article/details/18407527], very grateful for his valuable advice.
    because the ListView does not expose the interface to let us know whether the Onmesure () method executes, we only need to add a Boolean value to judge:
    The implementation of the code is:
 public  boolean  ismeasure;                                    @Override  protected  void  onmeasure  (int  widthmeasurespec, int  heightmeasurespec) {LOG.D ( "Onmeasure" ,                                             "Onmeasure" );                                            Ismeasure = true ;                                      //calculates the Itemview's width-height  ... }
  @Override protected void OnLayout (Boolean changed, int l                               , int t, int r, int b) {log.d ("OnLayout", "onlayout");                               Ismeasure = false;         Set Layout ...}                                  The next step is to modify the GetView () method of adapter: Public View getView (int position, View Convertview, ViewGroup parent) { ...//Determine if the object of the parent control is the view object you want to show if (parent instanceof My GridView) {if ((MyGridView) parent). Ismeasure ()) {RET                                    Urn Convertview;  }                             }          ...            } 
    • Then if you are going to implement a click event for the control inside, then you need to go through the event distribution, pass the parent control to the child control without consuming the event, and then the child item may have a preemption control, you can setfocus= "false" in the child control
      or the parent control android:descendantfocusability= "Afterdescendants"

    • I believe you should know how to do, this writing is also reference to other people's experience, we must have take doctrine spirit, the Chinese hand of the party is also a lot of, but this is not what, stepping on someone else's shoulder nothing bad, as long as you can progress every day, learn what you want

Resolve Listview,gridview GetView Multiple call issues

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.