Advanced Android--adapter in-depth understanding and optimization
Source: Internet
Author: User
<span id="Label3"></p><p style="text-align:center"><p style="text-align:center"><strong><span style="font-size:24px">Advanced Android--adapter in-depth understanding and optimization</span></strong><br></p></p><p><p><span style="font-size:18px"><br></span></p></p><p><p><span style="font-size:18px"><span style="color:#333333">typically for multiple elements, including</span> <span style="color:#333333">View</span> <span style="color:#333333">, such as</span> <span style="color:#333333">ListView</span> <span style="color:#333333">,</span> <span style="color:#333333">GridView</span> <span style="color:#333333">. </span> <span style="color:#333333">Expandablelistview</span><span style="color:#333333">, we are setting a</span> <span style="color:#333333">Adapter</span>for it<span style="color:#333333">. </span> <span style="color:#333333">Adapter</span> <span style="color:#333333">is</span> <span style="color:#333333"></span> a <span style="color:#333333">bridge that provides data</span> to and from view, and is also a view bridge that provides each <span style="color:#333333">Item</span> . <span style="color:#333333"> </span></span></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p style="text-align:center"><p style="text-align:center"><strong><span style="font-size:18px"></span></strong></p></p><p><p><span style="font-size:18px"><span style="color:#333333">in</span> <span style="color:#333333">ListView</span> <span style="color:#333333">as an Example. It works as</span> <span style="color:#333333">follows:</span></span></p></p><p><p><span style="font-size:18px"><span style="color:#333333"> <span style="white-space:pre"></span> ●</span> <span style="color:#333333">ListView</span> <span style="color:#333333">for</span> <span style="color:#333333">List</span> <span style="color:#333333">each of the</span> <span style="color:#333333">Item</span> <span style="color:#333333">,</span> <span style="color:#333333">Adapter</span> <span style="color:#333333">will call a</span> <span style="color:#333333">GetView</span> <span style="color:#333333">method to get a layout view</span></span></p></p><p><p><span style="font-size:18px"><span style="color:#333333"> <span style="white-space:pre"></span> ●</span> <span style="color:#333333">we will usually</span> <span style="color:#333333">Inflate</span> <span style="color:#333333">a new</span> <span style="color:#333333">View</span> <span style="color:#333333">, populates the data and returns the display</span></span></p></p><p><p><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><p><p><span style="font-size:18px"><span style="color:#333333">assuming, of course, that our</span> <span style="color:#333333">Item</span> <span style="color:#333333">very many words (for example, tens of Thousands) will create a new</span> <span style="color:#333333">View</span> <span style="color:#333333">it? It is very obvious that memory is not acceptable, and</span><span style="color:#333333">Google</span> <span style="color:#333333">will not do So. </span> <span style="color:#333333"></span> <span style="color:#333333">there's a widget called</span> <span style="color:#333333"><span style="color:#333333">recycler</span></span> in Android <span style="color:#333333">, and that's how he works:</span></span></p></p><p><p><span style="white-space:pre"></span></p></p><p style="text-align:center"><p style="text-align:center"><span style="white-space:pre"></span></p></p><p><p><span style="font-size:18px"></span></p></p><p><p><span style="font-size:18px"><span style="white-space:pre"></span>Very clearly, regardless of the number of item in the Data. On the display recycler only the visible view is stored in Memory.</span></p></p><p><p>When you swipe Down. The top invisible item moves directly back to the bottom to fill the data again into the new Item.</p></p><p><p>This way, you don't have to create a new view every Time.</p></p><p><p><span style="font-size:18px"><span style="white-space:pre"></span>This is the invocation of the GetView method that we have common in adapter, and we can see that Convertview is the layout view of each item before Recyler.</span></p></p><p><p><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><pre code_snippet_id="422142" snippet_file_name="blog_20140708_1_4365586" name="code" class="java"><pre code_snippet_id="422142" snippet_file_name="blog_20140708_1_4365586" name="code" class="java">Public View GetView (int position, View convertview, Viewgroupparent)</pre></pre><br><br><p><p></p></p><p><p><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><p><p><span style="font-size:18px"><span style="color:#333333">So. </span> <span style="color:#333333">Android</span> <span style="color:#333333">has provided us</span> with a <span style="color:#333333">recycler</span> <span style="color:#333333">mechanism, and we should use this mechanism instead of every time we go to</span> <span style="color:#333333">inflate</span> <span style="color:#333333">A </span> <span style="color:#333333">View</span><span style="color:#333333">. </span></span></p></p><p><p></p></p><p><p><span style="color:#333333"><span style="font-size:18px">Example</span></span></p></p><p><p><span style="background-color:rgb(255,255,255)"><span style="font-size:18px; color:#ff0000">Don ' t</span></span></p></p><p><p><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><pre code_snippet_id="422142" snippet_file_name="blog_20140708_2_2809454" name="code" class="java"><pre code_snippet_id="422142" snippet_file_name="blog_20140708_2_2809454" name="code" class="java">Public View GetView (int position, view convertview, viewgroupparent) { Convertview = Layoutinflater.from (mcontext). Inflate (r.layout.item_view,null); dosomething ... Return converview;}</pre></pre><br><br><pre><pre></pre></pre><p align="left"><p align="left"><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><p><p><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><p><p><span style="color:#333333"><span style="font-size:18px">Do</span></span></p></p><p><p></p></p><pre code_snippet_id="422142" snippet_file_name="blog_20140708_3_2685956" name="code" class="java"><pre code_snippet_id="422142" snippet_file_name="blog_20140708_3_2685956" name="code" class="java">Public View getView (int position, View convertview, viewgroupparent) { if (convertview ==null) { Convertview = Layoutinflater.from (mcontext). Inflate (r.layout.item_view, null); } dosomething ... Return converview;}</pre></pre><br><br><p><p></p></p><p align="left"><p align="left"><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><p align="left"><p align="left"><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><p align="left"><p align="left"><span style="font-size:18px">The role of Viewholder</span></p></p><p><p><span style="font-size:18px">The recycler model mentioned earlier is to solve the waste of view resources caused by repeated inflate, and what methods can we optimize our performance again? The answer is Yes.</span></p></p><p><p></p></p><p><p><span style="font-size:18px">We still look at each method call from getview, and find that in fact when we get convertview, every time we go to Findviewbyid according to this Layout. For example, let's make the usual notation:</span></p></p><p align="left"><p align="left"><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><p align="left"><p align="left"></p></p><pre code_snippet_id="422142" snippet_file_name="blog_20140708_4_675880" name="code" class="java"><pre code_snippet_id="422142" snippet_file_name="blog_20140708_4_675880" name="code" class="java">if (convertview = = Null) { Convertview = minflater.inflate (r.layout.item_view, null); } TextView Titletextview = (TextView) Convertview.findviewbyid (r.id.text)); ImageView Iconimageview = (ImageView) Convertview.findviewbutid (r.id.icon)); DoSomething ...</pre></pre><br><br><p><p></p></p><br><br><p><p></p></p><p><p><span style="font-size:18px"></span></p></p><p><p><span style="font-size:18px">Findviewbyid is in the analysis of the layout.xml layout of the sub-view, parsing XML is a definitely work, so Google also suggested that we optimize the thankless of the work, so put forward the concept of viewholder.</span></p></p><p align="left"><p align="left"><span style="font-size:18px"><span style="color:#333333">that</span> <span style="color:#333333">is, a static class is used to hold</span> the reference relationships for <span style="color:#333333">each child</span> <span style="color:#333333">View</span> in the <span style="color:#333333">XML</span> <span style="color:#333333">. This</span> eliminates the need to parse the <span style="color:#333333">XML</span> every time <span style="color:#333333">. For example, here is a viewholder for the above code</span> <span style="color:#333333"></span></span></p></p><p align="left"><p align="left"><span style="font-size:18px"><span style="color:#333333"><br></span></span></p></p><p align="left"><p align="left"></p></p><pre code_snippet_id="422142" snippet_file_name="blog_20140708_5_7153868" name="code" class="java"><pre code_snippet_id="422142" snippet_file_name="blog_20140708_5_7153868" name="code" class="java">Static class Viewholder { TextView titletextview; </pre></pre><br><br><p><p></p></p><br><p><p></p></p><p align="left"><p align="left"><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><p align="left"><p align="left"><span style="font-size:18px"><span style="color:#333333">But. In the</span> <span style="color:#333333">GetView</span> <span style="color:#333333">method We can only get three parameters,</span><span style="color:#333333">position</span><span style="color:#333333">,</span><span style="color:#333333">convertview</span><span style="color:#333333">,</span> <span style="color:#333333"> ViewGroup</span> <span style="color:#333333">is not the viewholder that we define ourselves</span> <span style="color:#333333"></span> <span style="color:#333333">. So. We hope</span> to <span style="color:#333333">get</span> <span style="color:#333333">viewholder</span> through <span style="color:#333333">convertview</span> <span style="color:#333333">only to put it in</span> the <span style="color:#333333">tag</span> <span style="color:#333333">. </span></span></p></p><p><p></p></p><p align="left"><p align="left"><span style="font-size:18px">the <span style="color:#333333">Following is a complete</span> <span style="color:#333333">Viewholder</span> <span style="color:#333333">Use</span> <span style="color:#333333">exmaple:</span></span></p></p><p align="left"><p align="left"><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><p align="left"><p align="left"><span style="color:#333333"><span style="font-size:18px"></span></span></p></p><pre code_snippet_id="422142" snippet_file_name="blog_20140708_6_392472" name="code" class="java"><pre code_snippet_id="422142" snippet_file_name="blog_20140708_6_392472" name="code" class="java"> Public View getView (int position, View convertview, viewgroup parent) { viewholder holder; if (convertview = = Null) { Convertview = minflater.inflate (r.layout.item_view, null); Holder = new Viewholder (); Holder.titletextview = (TextView) Convertview.findviewbyid (r.id.text); Holder.iconimageview = (ImageView) Convertview.findviewbyid (r.id.icon); Convertview.settag (holder); } else { holder = (viewholder) Convertview.gettag (); } Holder.titleTextView.setText (data[pos].title); Holder.iconImageView.setImageBitmap (data[pos].bitmap); return convertview; } Static class Viewholder { TextView titletextview; ImageView iconimageview; }</pre></pre><br><p><p></p></p><p><p><span style="font-size:18px"></span></p></p><p><p><span style="font-size:18px">Tips. The Recyclerview in Support.v7 is made with this Idea.</span></p></p><p><p></p></p><p align="left"><p align="left"><span style="font-size:18px">Multiple types of ViewType</span></p></p><p><p><span style="font-size:18px">When we call method GetView in adapter, assume that the item view in the entire list has multiple types of layouts, such as:</span></p></p><p align="center"><p align="center"><span style="font-size:18px"></span></p></p><p align="center" style="text-align:start"><p align="center" style="text-align:start"></p></p><p align="center" style="text-align:start"><p align="center" style="text-align:start"></p></p><p align="center" style="text-align:start"><p align="center" style="text-align:start"><br></p></p><p align="center"><p align="center"><span style="font-size:18px"></span></p></p><p align="left"><p align="left"><span style="font-size:18px">We continue to use Convertview to add data to the new population, which seems to be out of LINE. Because the Convertview type is different each time it is Returned. Cannot be Reused.</span></p></p><p align="left"><p align="left"><span style="font-size:18px">Android in the design of the Time. Also thought of This.</span></p></p><p><p>therefore, the two methods reserved in the Adapter.</p></p><p align="left"><p align="left"><span style="font-size:18px"></span></p></p><p align="left"><p align="left"></p></p><pre code_snippet_id="422142" snippet_file_name="blog_20140708_20_6891987" name="code" class="java"><pre code_snippet_id="422142" snippet_file_name="blog_20140708_20_6891987" name="code" class="java">public int Getitemviewtype (int position);</pre></pre><br><br><p><p></p></p><p align="left"><p align="left"><span style="font-size:18px"></span></p></p><pre code_snippet_id="422142" snippet_file_name="blog_20140708_21_9083814" name="code" class="java"><pre code_snippet_id="422142" snippet_file_name="blog_20140708_21_9083814" name="code" class="java">public int Getviewtypecount ();</pre></pre><br><p><p></p></p><p align="left"><p align="left"><span style="font-size:18px"></span></p></p><p align="left"><p align="left"><span style="font-size:18px">Just one more time, these two methods. Set the number of Itemviewtype and the inference Method. Recycler can selectively give different convertview.</span></p></p><p align="left"><p align="left"><span style="font-size:18px"></span></p></p><p align="left"><p align="left"><span style="font-size:18px">Example:</span></p></p><p align="left"><p align="left"><span style="color:rgb(51,51,51)"><span style="font-size:18px"></span></span></p></p><pre code_snippet_id="422142" snippet_file_name="blog_20140708_9_7251362" name="code" class="java"> @Override public intgetitemviewtype (int position) {if (data[pos].type = = 0) {return 0; } else {return 1; }} @Override public int getviewtypecount () {return 2; } @Override public View getView (int position, view convertview, viewgroup arg2) {titleviewholder Titleholder ; Infoviewholder infoholder; int type = Getitemviewtype (position); if (convertview = = Null) {switch (type) {case 0:convertview = minflater.inflate (R). layout.item_view, null); Titleholder = new Titleviewholder (); Titleholder.titletextview = (TextView) Convertview.findviewbyid (r.id.text); Titleholder.iconimageview = (ImageView) Convertview.findviewbyid (r.id.icon); Convertview.settag (titleholder); Break Case 1:convertview = minflater.inflate (r.layout.item_view2, null); Infoholder = new Infoviewholder (); Infoholder.titletextview = (TextView) Convertview.findviewbyid (r.id.text); Convertview.settag (infoholder); Break }} else {switch (type) {case 0:titleholder = (titleviewholder) Convertview . Gettag (); Break Case 1:infoholder = (infoviewholder) Convertview.gettag (); Break }} switch (type) {case 0:titleholder.titletextview.settext (data[pos].title); Break Case 1:infoholder.titletextview.settext (data[pos].title); InfoHolder.iconImageView.setImageBitmap (data[pos].bitmap); Break } return convertview; } static class Titleviewholder {public ImageView iconimageview; Public TextView titletextview; } static class Infoviewholder {TextView titletextview; ImageView iconimageview; }</pre><br><p><p></p></p><br><p><p></p></p><p align="left"><p align="left"><span style="font-size:18px"></span></p></p><p align="left"><p align="left"><span style="font-size:18px"><strong>notifydatasetchanged Refresh mechanism</strong></span></p></p><p><p><span style="font-size:18px">When the data in the ListView has Changed. When we want to refresh the view in the listview, we usually call notifydatasetchanged to refresh the LISTVIEW.</span></p></p><p><p>Take a look at its source code:</p></p><p><p><br></p></p><p><p></p></p><pre code_snippet_id="422142" snippet_file_name="blog_20140708_10_4044996" name="code" class="java"><pre code_snippet_id="422142" snippet_file_name="blog_20140708_10_4044996" name="code" class="java"> public void notifychanged () { synchronized (mobservers) { //send onchanged for (int i =) to each child view Mobservers.size ()-1; I >= 0; i--) { mobservers.get (i). onChanged ();}} }</pre></pre><br><br><p><p></p></p><br><p><p></p></p><p align="left"><p align="left"><span style="font-size:18px">It was found to be refreshed for each sub-view, assuming, of course, that our data variables are Understandable. But. Under normal conditions, We need to update the view is not many.</span></p></p><p><p>Frequent calls to the <span style="color:#333333">notifydatasetchanged</span> <span style="color:#333333">method. Refreshing the entire interface is not Appropriate. This will redraw all items displayed on the interface</span> <span style="color:#333333"></span> <span style="color:#333333">once, Even if only one</span> <span style="color:#333333">view</span> has <span style="color:#333333">Changed. </span></p></p><p><p></p></p><p align="left"><p align="left"><span style="font-size:18px">So. We are able to write an update method to refresh a view individually</span></p></p><p align="left"><p align="left"></p></p><pre code_snippet_id="422142" snippet_file_name="blog_20140708_5_9111936" name="code" class="java"><pre code_snippet_id="422142" snippet_file_name="blog_20140708_5_9111936" name="code" class="java">private void Updateview (int ItemIndex) { intvisibleposition = yourlistview.getfirstvisibleposition (); VIEWV = Yourlistview.getchildat (itemindex-visibleposition); Viewholder Viewholder = (viewholder) V.gettag (); If (viewholder!= null) { viewHolder.titleTextView.setText ("i updated");} }</pre></pre><br><br><p><p></p></p><p><p><span style="font-size:18px"></span></p></p><p align="left"><p align="left"><span style="font-size:18px"><strong>Network picture optimization in adapter</strong></span></p></p><p><p><span style="font-size:18px">Each item in the ListView will have a network image. When the item is more, too much network request and too much picture storage will be the ListView to slow the Change Card.</span></p></p><p><p><span style="font-size:18px"><br></span></p></p><p><p><span style="font-size:18px">So do the optimization for it:</span></p></p><p><p><span style="font-size:18px"></span></p></p><p><p><span style="font-size:18px">Using the thread pool for the network picture request, the network picture request obtains after uses the local cache processing (LRUCache). Memory + Local File Cache. of course, a weak reference (weakreference) is required to store the images in memory in order to prevent memory overflow and recycling.</span></p></p><p><p><span style="font-size:18px"></span></p></p><p><p><span style="font-size:18px">Scales the pictures taken in the network proportionally. To reduce memory Consumption.</span></p></p><p><p></p></p><p><p><span style="font-size:18px"></span></p></p><p><p><span style="font-size:18px">When you swipe, you don't need to request a web Image.</span></p></p><p><p>As a result, network requests are generally more time-consuming. A picture of an item, assuming it is recycler replaced when Requested. The picture will not be corresponding to the Item.</p></p><p><p></p></p><p><p><span style="font-size:18px"></span></p></p><p><p><span style="font-size:18px"></span></p></p><p><p><span style="font-size:18px">TIPS. The network Request tool analogy is more inconvenient to cite Example. however, the use of more frequent network Image request Tool class is volley, volley provides a Imageloader tool class and Networkimageview network picture request view</span></p></p><p><p><span style="font-size:18px"><br></span></p></p><p><p><span style="font-size:18px"><br></span></p></p><p><p><span style="font-size:18px"><br></span></p></p><p><p><span style="font-size:18px"></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-size:18px">/**<br>* @author Zhoushengtao (Zhou San)<br>* @since July 8, 2014 15:08:29<br>* @weixin stchou_zst</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-size:18px">* @blog http://blog.csdn.net/yzzst<br>*/</span></p></p><br> <p><p> Advanced Android--adapter in-depth understanding and optimization </p> </p></span>
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