当单击时更改列表视图项的背景

3

我有一个列表视图,希望当我点击一行时,它的背景变为蓝色。我使用了以下代码:

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);
                        }

                    });

这个功能有些问题。当我点击第一个项目时,它会变成蓝色,但第3和第5项也会变成蓝色!我不明白为什么!!我只想让选定的项目变成蓝色!!


使用选择器(搜索相关信息) - Houcine
尝试这个答案...http://stackoverflow.com/questions/15253987/inflate-listview-row-from-onclicklistener-in-android/15254297#15254297 - Pragnani
3个回答

2

使用选择器怎么样?它们可以正常工作并提供清晰的解决方案。

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>

使用方法

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

关键属性是为您的ListView设置android:listSelector="@drawable/listselector"

注意:

您也可以使用形状和渐变属性,而不是纯色。有关更多详细信息,您还可以查看教程Android Custom ListView


它可以工作,但当我点击一行时,颜色会改变,但当我从行中移开焦点时,颜色会重置。我想永久改变颜色。我该如何做到这一点? - Fcoder
嗨,仍然只有在项目点击时才更改背景。我正在使用自定义适配器,如何更改所选项目视图的文本视图颜色??? - Sampath Kumar

0
如果您有自定义列表视图,则使用以下代码。
  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;

}

您的自定义布局

 <?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>

在资源文件夹下创建一个drawable文件夹。将以下XML代码命名为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>

当名称为normal.xml的drawable处于正常状态时的形状

   <?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>

在drawable文件夹下以pressed.xml命名的情况下,形状在按下时

 <?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>

0

使用listselectors来实现这个目标:

这里有一个例子: http://www.michenux.net/android-listview-highlight-selected-item-387.html

如果你想让你的listview项目颜色永久改变,那么你需要创建一个选定位置的array,并在你自定义适配器的getview()方法中检查该位置是否存在于数组中,如果是,则手动更改视图的背景颜色。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接