After clicking on the Android ListView list item, keep the background color
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:12px">The project suddenly encountered such a problem, <strong><em>some message class interface, need to use the ListView to display, the normal requirement is just beginning to enter the message display interface, the list item display is all black, and then click on a list item, will be replaced by white, and keep the white display, </em></strong>the rest is still black, and at first it was a bit of a dead end, using Selector with shape to define the background display after the selection, unchecked, clicked, or clicked, as shown in the following example:</span></p></p><pre name="code" class="plain"><pre name="code" class="plain"></pre><p></p><pre name= "code" class= "html" ><?xml version= "1.0" encoding= "utf-8"?> <selector xmlns:android= "http://schemas.android.com/apk/res/android" > &L t;! --default background Picture--<item android:drawable= "@drawable/pic1"/> <!--background picture without Focus--<item Andro id:state_window_focused= "false" android:drawable= "@drawable/pic1"/> <!--non-touch mode to get focus and click on background picture <item android:state_focused= "true" android:state_pressed= "true" android:drawable= "@drawable/pic2"/> < !--touch mode when you click Background picture--<item android:state_focused= "false" android:state_pressed= "true" android:drawable= "@ DRAWABLE/PIC3 "/> <!--selected picture Background--<item android:state_selected=" true "android:drawable=" @drawable/pi C4 "/> <!--get Focus picture Background--<item android:state_focused=" true "android:drawable=" @drawable/pic5 "/> </selector> </pre></pre><br>The above code is provided by many predecessors, it is true that you can change the color after the click, but the problem comes, after clicking release the finger, will be displayed as the default background color, Unable to maintain the background color, thanks to Csdn's predecessors for the analysis of the problem, posted as Follows:<p><p></p></p><p><p><span style="font-family:monospace; white-space:pre; background-color:rgb(240,240,240)"><span style="font-size:24px; color:#cc0000">1.selector notation is grayed out when item is in selected state but the ListView defaults to default by clicking on item and releasing it, not in selected state like Tabwidget</span></span></p></p><p><p><span style="font-family:monospace; white-space:pre; background-color:rgb(240,240,240)"><span style="font-size:24px; color:#cc0000">The click of the 2.listview item is not clicked once the click is selected: Initial state click: Press the status button to Release: initial state</span></span><br></p></p><p><p><span style="font-family:monospace; white-space:pre; background-color:rgb(240,240,240)"><span style="font-size:24px; color:#cc0000"></span><span style="font-size:32px; color:#ff0000"><strong>This explains that I can't get the color through selector to finish the click, but another expert has proposed such a solution</strong></span> <span style="font-size:24px; color:#cc0000">:</span></span></p></p><span style="font-family:monospace; white-space:pre; background-color:rgb(240,240,240)"><span style="font-family:monospace; white-space:pre; background-color:rgb(240,240,240)"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:24px; background-color:rgb(245,245,245)"><span style="font-size:32px; color:#3333ff"><strong>In fact you are in the Onitemclicklistener of the listview, the currently selected item executes setselected (true).</strong></span></span></span></span><span style="font-family:monospace; white-space:pre; background-color:rgb(240,240,240)"><span style="font-family:monospace; white-space:pre; background-color:rgb(240,240,240)"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:24px; background-color:rgb(245,245,245)"><span style="font-size:32px; color:#3333ff"><strong><br></strong></span></span></span></span><span style="font-family:monospace; white-space:pre; background-color:rgb(240,240,240)"><span style="font-family:monospace; white-space:pre; background-color:rgb(240,240,240)"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:24px; background-color:rgb(245,245,245)"><span style="font-size:32px; color:#3333ff"><strong>According to his method, I clicked the function in the Listener list item to write the following code:</strong></span></span></span></span><span style="font-family:monospace; white-space:pre"><span style="font-family:monospace; white-space:pre"><span style="font-family:helvetica,tahoma,arial,sans-serif ; line-height:24px "> </span> </span></span><pre name="code" class="java" style="background-color: rgb(245, 245, 245);"><pre name="code" class="java" style="background-color:rgb (245, 245, 245);">listview listview = (listview) Getactivity (). Findviewbyid (r.id.listmessage); listview.setonitemclicklistener (new onitemclicklistener () {@ overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int arg2,long arg3) {alertdialog.builder Builder = n EW alertdialog.builder (getactivity ()); builder.seticon (r.drawable.ic_launcher); builder.settitle (maps.get (arg2). Get ("messagetitle"). toString ()); Messagetoaccount METOACC = tradeapi.getinstance (). querymessage ((String) maps.get (arg2). get ("guid")); byte[] Messagebyte = Metoacc.getcontext (); If (messagebyte! = Null) {try {new String (messagebyte, "UTF-8");} catch (unsupporteden Codingexception E) {e.printstacktrace ();}} Builder.setmessage (messagecontent), builder.setmessage ("the spot Transaction is the One-hand delivery of the transaction mode, or the use of barter trading method. Settlement within the time Limit. The difference between spot trading and other trading methods is that ① is intended to obtain ownership of the goods for the purpose of the Transaction. "); Builder.show ();}); </span> </pre></pre><span style="color:#3333ff; background-color:rgb(245,245,245)"><span style="color:#3333ff; background-color:rgb(245,245,245)"><strong><br></strong></span></span><span style="font-weight:bold">the <span style="font-weight:bold"><span style="font-size:32px; color:rgb(51,51,255); background-color:rgb(245,245,245)">Most critical thing is this code:</span></span></span><pre name="code" class="java" style="line-height: 23.99305534362793px;"><pre name="code" class="java" style="line-height: 23.99305534362793px;"> Arg1.setselected (true), which allows the color to remain the same after each click. <span style= "font-family:arial, Helvetica, sans-serif; font-size:12px; " > Now that the effect is complete, paste out my selector XML file:</span></span></pre></pre><pre name="code" class="java" style="line-height: 23.99305534362793px;"><?xml version= "1.0" encoding= "utf-8"?> <selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_selected=" true "> <shape> <!--gradient-- <gradient android:startcolor= "#B4B4B4" android:endcolor= "#B4B4B4" Android:type= "radial" android:gradientradius= "/> <!--strokes--- Lt;stroke android:width= "2dp" android:color= "#dcdcdc" android:dashwidth = "5dp" android:dashgap= "3dp"/> <!--rounded corners--<corners android:topleftradius= "5dp" android:toprightradius= "5dp" android:bottomleftradius= "5dp" android:bottomrightradius= "5dp"/> <padding android:left= "10dp" android:top= "10dp" android:right= "10dp" android:bottom= "10dp"/> </shape> </item> <item android:state_pressed= "true" > <shape> <!--fade-to-<g Radient android:startcolor= "#B4B4B4" android:endcolor= "#B4B4B4" Android : type= "radial" android:gradientradius= "/> <!--strokes--<stroke Android:width= "2dp" android:color= "#dcdcdc" android:dashwidth= "5dp" android:dashgap= "3dp"/> <!--rounded corners--<corners Android : topleftradius= "5dp" android:toprightradius= "5dp" android:bottomleftradius= "5dp" android:bottomrightradius= "5dp"/> <padding android:left= "10dp" A ndroid:top= "10dp" android:right= "10dp" android:bottom= "10dp"/> </shape> </item> <item> <shape> <solid android:color= "#2B2B2B"/> <str Oke android:width= "2dp" android:color= "#2B2B2B"/> <corners android:topleftradius= "5dp" android:toprightradius= "5dp" android:bottomleftradius= " 5DP "android:bottomrightradius=" 5DP "/> <padding android: left= "10dp" android:top= "10dp" android:right= "10dp" android:bottom= "10d P "/> </shape> </item> </selector></pre><br><br><pre><pre></pre></pre> <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>After clicking on the Android ListView list item, keep the background color</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