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 也变成蓝色!!!我不明白为什么!我只想要选定的项目变成蓝色!!!

4

3 回答 3

2

如何使用选择器?它们工作正常并提供干净的解决方案。

列表选择器.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>

正常的.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>

悬停.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"
/>

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

笔记:

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

于 2013-03-15T18:27:55.917 回答
0

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

于 2013-03-15T18:35:15.410 回答
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>

在资源下创建一个可绘制文件夹。将以下 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 的可绘制对象下正常时的形状

   <?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>
于 2013-03-15T18:33:22.627 回答