<span id="Label3"></p><p><p>The ListView is one of the most commonly used Android controls, allowing users to scroll off-screen data to the screen by swiping up and down, while the existing data on the screen scrolls out of the screen, for example, to display contact information, system settings, and so On.</p></p>ListView usage<p><p>There are two ways to implement the Listview:</p></p> <ol> <ol> <li>Configuring the ListView in XML</li> <li>Activity Inheritance Listactivity</li> </ol> </ol><p><p>The following are described Separately.</p></p>Configuring the ListView in XML<p><p>Add the following code to the Activity_main.xml:</p></p><pre class="prettyprint"><code class="language-xml hljs "><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">relativelayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span> > </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">ListView</span><span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/list_view"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match _parent "</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" Match_parent "</span><span class="hljs-attribute">android:headerdividersenabled</span>= <span class="hljs-value"> "false"</span><span class="hljs-attribute">android:listselector</span>=<span class="hljs-value">"#001166"</span><span class="hljs-attribute">android:divider</span>= "<span class="hljs-value">#ff00ff" </span> <span class="hljs-attribute">android:dividerheight</span>=<span class="hljs-value">"1dp"</span> /> </span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">relativelayout</span>></span></span></code></pre><p><p>Several of these important attributes:</p></p> <ul> <ul> <li>Android:listselector<br>Set the Color/picture when selected</li> <li>Android:divider<br>Set the color/picture between settings options</li> <li>Android:dividerheight<br>Set the height between settings options</li> <li>Android:entries<br>Setting option Data statically</li> </ul> </ul><p><p>After modifying the layout file, let's modify the corresponding activity Code:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-javadoc"><span class="hljs-javadoc">/**listview Exercise *<span class="hljs-javadoctag"> @author</span> sywyg *<span class="hljs-javadoctag"> @time</span> 2015/4/12 *</span> *</span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Adapterview </span>. <span class="hljs-title">Onitemclicklistener</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>ListView lv;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main); LV = (ListView) Findviewbyid (r.id.list_view); string[] cities = {<span class="hljs-string"><span class="hljs-string">"nanjing"</span></span>,<span class="hljs-string"><span class="hljs-string">"beijing"</span></span>,<span class="hljs-string"><span class="hljs-string">"shanghai"</span></span>,<span class="hljs-string"><span class="hljs-string">"guangzhou"</span></span>,<span class="hljs-string"><span class="hljs-string">"shenzhen"</span></span>,<span class="hljs-string"><span class="hljs-string">"xuzhou"</span></span>,<span class="hljs-string"><span class="hljs-string">"feng"</span></span>};<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Arrayadapter Parameter * 1. Context object * 2. The ID of the ListView child layout * 3. An array that needs to be adapted *</span> */</span> <span class="hljs-comment"><span class="hljs-comment">/*arrayadapter<string> adapter = new Arrayadapter<string> (this,android. r.layout.simple_list_item_1,names); Set the option to radio arrayadapter<string> adapter = new Arrayadapter<string> (this,android. r.layout.simple_list_item_single_choice,names); Lv.setchoicemode (listview.choice_mode_single); Set the option to multi-select arrayadapter<string> adapter = new Arrayadapter<string> (this,android. r.layout.simple_list_item_multiple_choice,cities); Lv.setchoicemode (listview.choice_mode_multiple); * *</span></span> <span class="hljs-comment"><span class="hljs-comment">//get Adapter</span></span>arrayadapter<string> adapter =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Arrayadapter<string> (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>Android. r.layout.simple_list_item_1,cities);<span class="hljs-comment"><span class="hljs-comment">//bind Adapter</span></span>Lv.setadapter (adapter);<span class="hljs-comment"><span class="hljs-comment">//event Monitoring</span></span>Lv.setonitemclicklistener (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); }<span class="hljs-javadoc"><span class="hljs-javadoc">/** *<span class="hljs-javadoctag"> @param</span> adapterview Current ListView *<span class="hljs-javadoctag"> @param</span> View may be a control or layout *<span class="hljs-javadoctag"> @param</span> POS Ition the first selected *<span class="hljs-javadoctag"> @param</span> ID selected ID</span> */</span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Onitemclick</span></span>(adapterview<?> adapterview, View view,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Position<span class="hljs-keyword"><span class="hljs-keyword">Long</span></span>Id) {listview listview = (listview) adapterview;<span class="hljs-comment"><span class="hljs-comment">//decide whether to select or revoke a selection</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span>ischecked = listview.isitemchecked (position); Listview.setselection (position);<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(ischecked) {toast.maketext (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>, ((TextView) view). getText (). toString (), toast.length_short). show (); }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{toast.maketext (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>,<span class="hljs-string"><span class="hljs-string">"undo selection"</span></span>, toast.length_short). show (); } }}</code></pre><p><p>In the <code>onCreate()</code> first place we created the adapter object Arrayadapter instance, where the ID parameter of the ListView child layout is the system-provided layout file, In fact we can completely define it ourselves, this follow-up introduction. Then call the ListView <code>setAdapter()</code> method to set the adapter Object. finally, We also set the Click event for the ListView (implement adapterview.onitemclicklistener, and Overwrite <code>onItemClick()</code> ).</p></p><p><p>We use the Arrayadapter adapter here, We use the Simpleadapter adapter to achieve Illustrated.</p></p>Simpleadapter<p><p>The Simpleadapter adapter requires an additional layout file to configure the listview, creating a new layout file item.xml, with the following code:</p></p><pre class="prettyprint"><code class="language-xml hljs "><span class="hljs-pi"><span class="hljs-pi"><?xml version= "1.0" encoding= "utf-8"?></span></span><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">linearlayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute"> Android:layout_width</span>=<span class="hljs-value">"match_parent"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span> <span class="hljs-attribute"> Android:orientation</span>=<span class="hljs-value">"horizontal"</span>> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">ImageView</span><span class="hljs-attribute">android:id</span>= "<span class="hljs-value">@+id/iv"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">" Wrap_content "</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" wrap_content "</span> /> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">TextView</span><span class="hljs-attribute">android:id</span>= "<span class="hljs-value">@+id/tv"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"wrap_content "</span><span class="hljs-attribute">android:layout_height</span>="<span class="hljs-value">wrap_content "</span> /> </span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">linearlayout</span>></span></span></code></pre><p><p>There are two controls, corresponding to pictures and Text.</p></p><p><p>For the main XML there is still only a ListView control, which is no longer given Here.</p></p><p><p>The Simpleadapter instantiation needs to receive five parameters, respectively:</p></p> <ol> <ol> <li>Context<br>The context that the ListView displays</li> <li>List Object<br>Store all the ListView options that you want to display. Each element corresponds to a map object, and each Map object represents a row in the listview, and the elements in the 4th parameter string array correspond before and after.</li> <li>Int<br>Resource identifier of a view layout that defines the views for this list Item.<br>Defines the layout file ID for each option in the listview, which is the ID of the new layout file Item.xml Above.</li> <li>string[]<br>Corresponds to the key of the Map object in the 2nd Parameter.</li> <li>int[]<br>Corresponds to the control ID in the layout in the 3rd Parameter.</li> </ol> </ol><p><p>A simple code is given below:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-comment"><span class="hljs-comment">//1. Add ListView option, Each option is picture and text</span></span>Map<string,object> item1 =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>hashmap<string, object> (); Item1.put (<span class="hljs-string"><span class="hljs-string">"image"</span></span>, r.drawable.abc_ab_share_pack_mtrl_alpha); Item1.put (<span class="hljs-string"><span class="hljs-string">"name"</span></span>,<span class="hljs-string"><span class="hljs-string">"sywyg"</span></span>); Map<string,object> item2 =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>hashmap<string, object> (); Item2.put (<span class="hljs-string"><span class="hljs-string">"image"</span></span>, r.drawable.abc_btn_check_material); Item2.put (<span class="hljs-string"><span class="hljs-string">"name"</span></span>,<span class="hljs-string"><span class="hljs-string">"wyg"</span></span>);<span class="hljs-comment"><span class="hljs-comment">//map list Items added to the lists</span></span>list< map<string,object>> list =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>arraylist< map<string,object>> (); list.add (item1); list.add (item2);<span class="hljs-comment"><span class="hljs-comment">//2. Mapping Relationships</span></span>String[] from = {<span class="hljs-string"><span class="hljs-string">"image"</span></span>,<span class="hljs-string"><span class="hljs-string">"name"</span></span>};<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>[] to = {r.id.iv,r.id.tv};<span class="hljs-comment"><span class="hljs-comment">//3. Set the adapter, the parameters are described above</span></span>Simpleadapter Simpleadapter =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Simpleadapter (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>, list,r.layout.item,from,to);</code></pre><p><p>With the above processing, a Simpleadapter object is created, followed by a method called the ListView <code>setAdapter()</code> . finally, set the Click event for the ListView (as in the previous method).</p></p>Customizing the ListView Interface<p><p>Although it can be illustrated through simpleadapter, but to achieve a more complex layout, we need to implement a custom adapter, we can inherit arrayadapter,simpleadapter,baseadapter, Here we choose to inherit the Baseadapter abstract class.</p></p><p><p>The implementation code is as Follows:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">myadapter</span><<span class="hljs-title">T</span>> <span class="hljs-keyword">extends</span> <span class="hljs-title">baseadapter</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>List<t> mobjects;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Context mcontext;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Resource<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Myadapter</span></span>(context context,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Resource,list<t> Objects) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(); Mcontext = context;<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. Resource = resource; Mobjects = Objects; }<span class="hljs-comment"><span class="hljs-comment">//gets The total number of list items</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span> <span class="hljs-title"><span class="hljs-title">GetCount</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Mobjects.size (); }<span class="hljs-comment"><span class="hljs-comment">//get list Items</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>Object<span class="hljs-title"><span class="hljs-title">GetItem</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Position) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Mobjects.get (position); }<span class="hljs-comment"><span class="hljs-comment">//get list item ID</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Long</span></span> <span class="hljs-title"><span class="hljs-title">Getitemid</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Position) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Position }<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Returns a child view that will be called when each subkey is scrolled into the screen * <span class="hljs-javadoctag">@param</span> Position selected location * <span class="hljs-javadoctag">@param</span> convertview Conversion View for caching a previously loaded layout * through which you can optimize the ListView to improve performance * <span class="hljs-javadoctag">@param</span> Parent Parents Component</span> */</span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>View<span class="hljs-title"><span class="hljs-title">GetView</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>position, View convertview, viewgroup parent) {view view =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>; Viewholder Viewholder =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(convertview = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-comment"><span class="hljs-comment">//gets The layout that displays the ListView subkey</span></span>View = Layoutinflater.from (mcontext). Inflate (resource,<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>);<span class="hljs-comment"><span class="hljs-comment">//get controls in Sub-layouts</span></span>VIEWHOLDER.IV = (ImageView) View.findviewbyid (r.id.iv); viewholder.tv = (TextView) View.findviewbyid (r.id.tv);<span class="hljs-comment"><span class="hljs-comment">//save the Viewholder object in tag</span></span>View.settag (viewholder); }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{<span class="hljs-comment"><span class="hljs-comment">//avoid Reloading The layout each time the method is called</span></span>View = convertview;<span class="hljs-comment"><span class="hljs-comment">//remove Viewholder Object</span></span>Viewholder = (viewholder) View.gettag (); }<span class="hljs-comment"><span class="hljs-comment">//get options for position location</span></span>City City = (city) Mobjects.get (position);<span class="hljs-comment"><span class="hljs-comment">//set Control Values</span></span>ViewHolder.iv.setImageResource (city.getimage ()); ViewHolder.tv.setText (city.getname ());<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>View }<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Cache control instances, Avoid instantiating controls multiple times</span> */</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span>Class Viewholder{ImageView iv; TextView tv; }}</code></pre><p><p>Inheriting the Baseadapter abstract class requires implementing the above 4 methods, in addition the context, the ListView Child layout id, and the data in the constructor are passed IN. Let's focus on the <code>getView()</code> method below.</p></p><p><p><code>getView()</code>Used to return the view, which is called when each subkey is scrolled into the screen (the number of first calls to the screen can display the count of children, of course, the width of the ListView is determined, otherwise it will be repeated calls to determine the width of the high), The position value is cycled from 0 to the maximum number of list items that the screen can display (the maximum number of displays on the screen should be exactly the number plus 1). In it, the conversion view Convertview is used to cache a sub-layout that was previously loaded and is not currently displayed on the screen (when the position reaches its maximum, it caches the No. 0 one), which optimizes the listview, improves performance, The layout control is then cached through an inner class viewholder to avoid being called each time the view is loaded <code>findViewById()</code> .</p></p><p><p>After we have created the adapter class, we also work with the ListView in the activity just now, except that the adapter we are using is our custom adapter. The code is no longer given.</p></p>Activity Inheritance Listactivity<p><p>We can also implement the ListView by inheriting listactivity, This method does not require us to provide a ListView view, just set the adapter, the listener can also pass the method of the class, as Follows:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">listactivitytest</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">listactivity</span>{</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate);<span class="hljs-comment"><span class="hljs-comment">//create Adapter</span></span>string[] cities = {<span class="hljs-string"><span class="hljs-string">"nanjing"</span></span>,<span class="hljs-string"><span class="hljs-string">"beijing"</span></span>,<span class="hljs-string"><span class="hljs-string">"shanghai"</span></span>,<span class="hljs-string"><span class="hljs-string">"guangzhou"</span></span>,<span class="hljs-string"><span class="hljs-string">"shenzhen"</span></span>,<span class="hljs-string"><span class="hljs-string">"xuzhou"</span></span>,<span class="hljs-string"><span class="hljs-string">"feng"</span></span>}; arrayadapter<string> adapter =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Arrayadapter<string> (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>Android. r.layout.simple_list_item_2,cities);<span class="hljs-comment"><span class="hljs-comment">//get the activity in the ListView</span></span> <span class="hljs-comment"><span class="hljs-comment">//listview ListView = Getlistview ();</span></span> <span class="hljs-comment"><span class="hljs-comment">//set the ListView adapter</span></span>Setlistadapter (adapter); }<span class="hljs-javadoc"><span class="hljs-javadoc">/** * ListView Event monitoring, or by inheriting Adapterview.onitemclicklistener * Each parameter and Adapterview.onitemclicklistener method Onitemclick corresponding The Same. *<span class="hljs-javadoctag"> @param</span> L *<span class="hljs-javadoctag"> @param</span> v *<span class="hljs-javadoctag"> @param</span> position *<span class="hljs-javadoctag"> @param</span> ID</span> */</span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Onlistitemclick</span></span>(ListView l, View v,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Position<span class="hljs-keyword"><span class="hljs-keyword">Long</span></span>Id) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. Onlistitemclick (l, v, position, id); TextView TextView = (TextView) v; Toast.maketext (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>, Textview.gettext (). toString (), toast.length_short). show (); }}</code></pre><p><p>This does not require us to configure the layout, but rather to take advantage of the layout provided by listactivity, and of course we can provide one, but must contain an ID for <code>android:id="@android:id/list"</code> the listview Control.</p></p>Optimization <ol> <ol> <li>Improve performance with CONVERTVIEW.</li> <li>In the XML configuration, the ListView control is fixed with its width and height so that the view is not drawn multiple times (if the width is indeterminate, it is repeatedly determined), resulting in multiple invocations <code>getView()</code> .</li> <li>The internal class Viewholder is used to identify some controls in the view, without each invocation <code>findViewById()</code> , improving performance. Setting Viewholder to static only takes a long time to load for the first time, while ensuring that there is only one viewholder in memory, saving memory Overhead.</li> <li>Other...</li> </ol> </ol> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p> ListView for Android Control </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