change listview item background when clicke

醉酒当歌 提交于 2019-12-06 03:10:34

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!