[英]Android ListView Item Background Color
I am trying to allow the user to select more than one item. 我试图允许用户选择多个项目。 I want to "highlight" each list item selected so you can tell which item(s) have been selected.
我要“突出显示”每个选定的列表项,以便您可以知道已选择了哪个项目。
I have tried: view.setBackgroundResource(); view.setBackgroundColor(); view.setBackgroundDrawable();
我试过了:
view.setBackgroundResource(); view.setBackgroundColor(); view.setBackgroundDrawable();
view.setBackgroundResource(); view.setBackgroundColor(); view.setBackgroundDrawable();
I havent had any success. 我还没有成功。
Thannks for the help! 感谢您的帮助!
u can overwrite setOnTouchListener in your adapter: 您可以在适配器中覆盖setOnTouchListener:
view.setOnTouchListener(new OnTouchListener() { view.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
//the bacground when u select item
v.setBackgroundResource(android.R.color.holo_blue_light);
break;
case MotionEvent.ACTION_UP:
//设置背景为未选中正常状态
v.setBackgroundResource(android.R.color.background_light);
break;
default:
v.setBackgroundResource(R.drawable.mm_listitem_simple);
break;
}
return false;
}
you can set diffrence background when your action hanppend. 您可以在动作增强时设置差异背景。
You need to use a custom array adapter , if you are using a listview with only one element you MAY be able to get away with some implemented listview method, but you will have much more flexibility and less headache if you make a custom array adapter instead. 您需要使用自定义数组适配器,如果您仅使用一个只有一个元素的列表视图,则可以使用一些已实现的listview方法,但是如果您改用自定义数组适配器,则将具有更大的灵活性和更少的麻烦。 You can access all elements in it, each element can have its own onClickListener and everything
您可以访问其中的所有元素,每个元素可以具有自己的onClickListener以及所有内容
If you are passing a String array to the adapter,you can create custom adapter like this and can change background color of a selected item as below: 如果要将String数组传递给适配器,则可以创建这样的自定义适配器,并可以如下更改所选项目的背景色:
set the adapter to listview like: 将适配器设置为listview,例如:
String[] options={"abc","def","ghi","jkl"};
CustomAdapter ca=new CustomAdapter(this,options);
listView.setAdapter(ca);
and here is the custom adapter class: 这是自定义适配器类:
public class CustomAdapter extends BaseAdapter
{
String items[];
LayoutInflater mInflater;
Context context;
public CustomAdapter(Context context,String[] items)
{
this.items=items;
this.context=context;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
convertView = mInflater.inflate(R.layout.cutsom_listitem, null);
holder = new ViewHolder();
holder.itemName=(TextView)convertView.findViewById(R.id.itemName);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
String option=items[position];
holder.itemName.setText(option);
holder.itemName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.itemName.setBackgroundColor(Color.parseColor("#FF0000")); // making selected item red colored
}
});
return convertView;
}
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int position) {
return items[position];
}
@Override
public long getItemId(int position) {
return position;
}
}
public static class ViewHolder
{
TextView itemName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.