简体   繁体   中英

change listview item background when clicke

I have a listview and i want to when i click on a row, its background changes to blue. i use this code:

listView1.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
                            // TODO Auto-generated method stub
                            parent.getChildAt(position).setBackgroundColor(Color.BLUE);
                        }

                    });

this works wit some wrong. when i click on first item, it turns to blue but item #3 and #5 changes to blue too!!! i cant understand why!! i just want only selected item turns to blue!!!

What about to use selectors? They works properly and provide clean solution.

listselector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/normal" />

    <item
        android:state_selected="true"
        android:state_focused="false"
        android:drawable="@drawable/hover" 
        />

    <item 
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/hover" />
</selector>

normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
     >
    <solid 
        android:color="#cccccc"
        />
</shape>

hover.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
     >
    <solid 
        android:color="#dddddd"
        />
</shape>

An Usage

<ListView
   android:id="@+id/list"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:listSelector="@drawable/listselector"
/>

Key property is to set android:listSelector="@drawable/listselector" for your ListView .

Note:

You can use in shape also gradient property instead of solid color. And for more details also you can look at tutorial Android Custom ListView .

If you have a custom list view then use the below code.

  public View getView(final int arg0, View arg1, ViewGroup arg2) {
final ViewHolder vh;
vh= new ViewHolder();

if(arg1==null )
{
                arg1=mInflater.inflate(R.layout.lyourcustomlayouttobe inflated, arg2,false);//custom layout inflated
        arg1.setTag(vh);
        }

return arg1;

}

Your custom layout

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="horizontal"
 android:cacheColorHint="#000000"
 android:background="@drawable/listviewbkg">
 //other items to be inlfated.
 </LinearLayout>

Create a drawable folder under resources. post the below xml as listviewbkg

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true" 
 android:drawable="@drawable/pressed" />
 <item  android:state_focused="false" 
 android:drawable="@drawable/normal" />
 </selector>

Shape when normal under drawable with name normal.xml

   <?xml version="1.0" encoding="UTF-8"?> 
   <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
   <solid android:color="#FFFFFF"/>//change color    
    <stroke android:width="3dp"
    android:color="#0FECFF" /><!-- #330000FF #ffffffff -->//border color
   <gradient                               // remove the gradient if do not wish to use.
    android:startColor="#ffffffff" 
    android:endColor="#110000FF" 
    android:angle="90"/> 

    <padding android:left="5dp"
     android:top="5dp"
     android:right="5dp"
     android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp"      // change this to increase the rounded edge radius
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp"
     android:topRightRadius="7dp"/> 
     </shape>

Shape when pressed under the name pressed.xml in drawable folder

 <?xml version="1.0" encoding="UTF-8"?> 
  <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  <solid android:color="#FF1A47"/>    //change color 
  <stroke android:width="3dp"
    android:color="#0FECFF"/>//border color
  <padding android:left="5dp"
     android:top="5dp"
     android:right="5dp"
     android:bottom="5dp"/> 
  <corners android:bottomRightRadius="7dp"// increase the radius at the edge
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp"
     android:topRightRadius="7dp"/> 
  </shape>

use listselectors to achieve this goal:

Hree is an example: http://www.michenux.net/android-listview-highlight-selected-item-387.html

and if you want your listview item color permanent, then, you need to create an array of selected positions, an in getview() method of your cutom adapter, you need to check if that position is exists in array or not, if yes, then change background color of your view manually

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM