如何使用AdapterView更改ListView上所选项目的颜色

时间:2015-03-10 14:23:06

标签: android android-listview

我正在研究如何更改ListView上所选项目的颜色,因此我可以为用户提供更好的方式来使用我的应用程序,因此当单击ListView项目时,项目的颜色会发生变化,或任何很酷的动画。

我使用adapterView作为我的Listview:这是代码:



public class adapterq extends ArrayAdapter<Questionaire> {
Bitmap image;


public adapterq(Context context, ArrayList<Questionaire> questionaires) {
   super(context, 0, questionaires);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
   
   final Questionaire c = getItem(position);    
   
   View  convertView2;
   if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.customquest, parent, false);
      convertView2 = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
   }else{
    convertView2 = (View) convertView.getTag();
   }




   TextView q = (TextView) convertView.findViewById(R.id.textView1);
   final EditText name =     (EditText) convertView2.findViewById(R.id.editText1);


   q.setText(c.getLabel()); 

   convertView.setOnClickListener(new OnClickListener(){
 @Override
     public void onClick(View v) {
      Intent intent = new Intent();
      intent.setClass(getContext(), Questions.class);
      intent.putExtra("name", name.getText().toString());
      intent.putExtra("category", c.getCode());
      getContext().startActivity(intent);  
      
   
    		 

    }

    });
   convertView.setTag(convertView2);
   return convertView;
}

}
&#13;
&#13;
&#13;

以下是我惊人的Listview的截图:

enter image description here

1 个答案:

答案 0 :(得分:2)

你可以在你的onClick中使用:

v.setBackgroundResource(R.drawable.yourbackground);

并在drawable文件夹中创建yourbackground.xml,如下所示:

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

    <gradient
        android:angle="-270"
        android:endColor="#781704"
        android:startColor="#A61E03" />

</shape>