Analysis of the reuse mechanism of Androidadapterviewview (reproduced)

Source: Internet
Author: User
<span id="Label3"></p>Analysis of reuse mechanism of Androidadapterviewview<br><p><p><span style="white-space:pre"></span>For the listview, the GridView believe that everyone is not unfamiliar, rewrite a baseview, to achieve a few methods, and then completed our interface display, and in most cases, we load a special lot of item will not happen oom, we also understand the internal cache mechanism, Have encountered a number of Itemview multiplexing problems, such as loading images asynchronously, resulting in the interface display confusion, we generally use settag, and then callback display, to avoid Confusion.</p></p><p><p><span style="white-space:pre"></span>Scenario 1: take the ListView as an example, if the ListView itemview multiplexing mechanism, all Itemview multiplexing the same, if in the case of multi-threaded download picture, may eventually only the last view display picture bar, because your previous settings Settag (url ), the value of your tag will be overwritten immediately after, and finally Findviewbytag find is the last One. This shows that the ListView cache is not a, at least one screen can display the Number. In other words, the ListView maintains a Itemview Pool.</p></p><p><p><span style="white-space:pre"></span>Explain why the cache of a screen can show the largest number of Itemview pool, we may be thousands of itemview, just rely on the tag can be achieved without confusion.<span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>Scenario: each time the screen shows 7 Item,listview total of 1000 item, each item shows a picture downloaded from the Network.</p></p><p><p><span style="white-space:pre"></span>The code for GetView is about This:</p></p><p><p><span style="white-space:pre"></span></p></p><pre code_snippet_id="307367" snippet_file_name="blog_20140423_1_5250437" name="code" class="java"><pre code_snippet_id="307367" snippet_file_name="blog_20140423_1_5250437" name="code" class="java">@Overridepublic View GetView (int position, view convertview, viewgroup parent) {final String url = getItem (position); View view;if (convertview = = Null) {view = layoutinflater.from (getcontext ()). Inflate (r.layout.photo_layout, null);} Else{view = convertview;} Final ImageView photo = (ImageView) View.findviewbyid (r.id.photo);//set a tag for ImageView to ensure that images are not randomly loaded when loading asynchronously Photo.settag (url ); new Loadimgtask (photo). execute (url); return view;}</pre></pre>Download the completed image to Photo.gettag (). equals (url) to prevent clutter in the display of Pictures.<p><p></p></p><p><p>If we open the interface, opened 7 threads to download, at this time the cache of the 7 itemview, now slide screen display another screen, at this time 7 Itemview will be reused, will be the first screen set tag all covered out, Yes is covered out, and opened 7 threads to download pictures, When the first screen of the Itemview picture download completed, If the direct Findviewbytag and then set the picture will be displayed on the second screen, it is chaotic, so generally before the display will judge Photo.gettag (). equals (url); That is to say the first screen of the Itemview picture downloaded, but tag is overwritten, so even if the download is complete, there will be no display. This explains why we prevent confusing code from having to write like That.</p></p><p><p><span style="white-space:pre"></span>okay, Here's A look at the source from the perspective of how the ListView is Cached:</p></p><p><p><span style="white-space:pre"></span>Following the listview, entering the parent class abslist, you will find such a variable:</p></p><p><p><span style="white-space:pre"></span></p></p><pre code_snippet_id="307367" snippet_file_name="blog_20140423_2_4073148" name="code" class="java"><pre code_snippet_id="307367" snippet_file_name="blog_20140423_2_4073148" name="code" class="java">/** * The data set used to store unused views this should be reused during the next layout * to avoid creating new Ones * /final recyclebin mrecycler = new RecycleBin ();</pre></pre><br>The meaning of the note is to use a dataset to store the view that should be reused in the next layout, avoiding recreating the new Layout. This object should be the core class for our cache Management. Keep looking at this class, this is an inner class:<p><p></p></p><p><p></p></p><pre code_snippet_id="307367" snippet_file_name="blog_20140423_3_7039534" name="code" class="java">/** * The RecycleBin facilitates reuse of views across Layouts. The recyclebin has a levels of * storage:activeviews and Scrapviews. Activeviews is those views which were onscreen at the * start of a Layout. by construction, They is displaying current Information. At the end of "layout", all views in Activeviews is demoted to Scrapviews. Scrapviews is old views that * could potentially is used by the adapter to avoid allocating views unnecessarily. * * @see android.widget.abslistview#setrecyclerlistener (android.widget.AbsListView.RecyclerListener) * @see Andro Id.widget.AbsListView.RecyclerListener */class recyclebin {private view[] mactiveviews = new view[0]; Private arraylist& Lt view>[] mscrapviews;<span style="white-space:pre"><span style="white-space:pre"></span></span>.... }</pre>General Meaning: This class is used to help reuse view when sliding layouts, RecycleBin contains two levels of storage, Activeviews and Scrapviews,activeviews store The view that is displayed on the screen for the first time All activeviews will eventually be moved to scrapviews,scrapviews storage is a view that is likely to be adapter Reused.<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Now it's clear that the Abslistview cache relies on two arrays, an array that stores the itemview of the current reality on the screen, a itemview that is removed from the screen and may be reused. Look at the code inside the Listview:<p><p></p></p><p><p><span style="white-space:pre"></span></p></p><pre code_snippet_id="307367" snippet_file_name="blog_20140423_4_1085716" name="code" class="java"><pre code_snippet_id="307367" snippet_file_name="blog_20140423_4_1085716" name="code" class="java">@Override protected void Layoutchildren () {if (datachanged) {for (int i = 0; i < childCount; i++) {RECYCLEBIN.ADDSC Rapview (getchildat (i));} } else {recyclebin.fillactiveviews (childCount, firstposition);} ....}</pre></pre><br><pre code_snippet_id="307367" snippet_file_name="blog_20140423_5_2415899" name="code" class="java"><pre code_snippet_id="307367" snippet_file_name="blog_20140423_5_2415899" name="code" class="java">/*** Fill activeviews with all of the children of the abslistview.** @param childCount the minimum number of views mactive Views should hold* @param firstactiveposition the position of the first view that would be stored in* mactiveviews*/voi D fillactiveviews (int childCount, int Firstactiveposition) {if (mactiveviews.length < ChildCount) {mactiveviews = new V iew[childcount];} Mfirstactiveposition = firstactiveposition;final view[] activeviews = mactiveviews;for (int i = 0; i < childCount; i++) {View child = Getchildat (i); activeviews[i] = child;}}</pre></pre><br>It can be seen that if the data changes in the current itemview into the scrapviews, or the current display itemview into Activeviews. So where exactly is the key GetView method called, See the method in RecycleBin below:<p><p></p></p><p><p></p></p><pre code_snippet_id="307367" snippet_file_name="blog_20140423_6_7890540" name="code" class="java">/** * Get A view and has it show the data associated with the specified * Position. This was called when we had already discovered that the view was * not available for reuse in the recycle bin. The only choices left is * converting an old view or making a new one. * * @param position the position to display * @param isscrap Array of at least 1 boolean, the first entry would BEC ome true if * The returned view is taken from the scrap heap, false if Otherwise. * * @return A View displaying the data associated with the specified position */view obtainview (int position, boolean[] Isscrap) {isscrap[0] = false; View scrapview; Scrapview = Mrecycler.getscrapview (position); View child; If (scrapview! = Null) {child = Madapter.getview (position, scrapview, this); If (child! = Scrapview) {mrecycler.addscrapview (scrapview); } else { isscrap[0] = true; Child.dispatchfinishtemporarydetach (); }} Else {child = Madapter.getview (position, null, this); } return child; }</pre><br>As can be seen, this method is to return the current layout of the User's current item display, first according to position to Scrapview in search, find and call our getview, at this time GetView inside convertview!=null, Then GetView if the returned view changes, cache it down, otherwise Convertview==null.<br><span style="white-space:pre"><span style="white-space:pre"></span></span><p><p></p></p><p><p>well, mainly to let you know, abslistview why we can be set by a tag to ensure its correct display, and the caching mechanism in Abslistview exactly how to achieve, given that the source code is too long, can only approximate according to the code to understand the Principle.</p></p><p><p><br></p></p><p><p>finally, you crossing, Nothing left a word, the top of a Bai ~</p></p><p><p><br></p></p><p><p><br></p></p><p><p><br></p></p><p><p><br></p></p>Original link This article by Bean John Blog Backup expert remote One click release<br><p><p>Analysis of the reuse mechanism of Androidadapterviewview (reproduced)</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.