android中设置ListView的选中的Item的背景颜色

ListView中没有默认的选择颜色,只有选择Item后的焦点颜色,鼠标点击时Item有颜色,放开鼠标后颜色也就没有了,要实现放开鼠标后选择项的背景还是有颜色的。

  

   

1、配置main.xml

Java代码 复制代码 收藏代码
  1. "1.0" encoding="utf-8"?> 
  2. "http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"
  5.     "@+id/listView" android:listSelector="#000000" 
  6.         android:layout_width="fill_parent" 
  7.         android:layout_height="wrap_content" /> 
  8.  


	

    设置Item的获取焦点颜色为白色android:listSelector="#000000"(即不显示背景颜色)

2、配置用于ListView显示Item的button_layout.xml

Java代码 复制代码 收藏代码
  1. "1.0" encoding="utf-8"?> 
  2.   xmlns:android="http://schemas.android.com/apk/res/android" 
  3.   android:id="@+id/LinearLayoutButton" 
  4.   android:layout_width="144px" 
  5.   android:layout_height="99px" 
  6.   android:gravity="center" 
  7.   android:orientation="vertical"
  8.    
  9.      
  10.    
  11.         android:id="@+id/TextViewButton"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="fill_parent" 
  14.         android:textSize="20px"
  15.      
  16.      
  17.      
  18.  


  
	
	
	
	
	

3、实现Activity

Java代码 复制代码 收藏代码
  1. package com.listButtonTest.www; 
  2.  
  3. import java.util.ArrayList; 
  4.  
  5. import android.app.Activity; 
  6. import android.content.Context; 
  7. import android.graphics.Color; 
  8. import android.os.Bundle; 
  9. import android.os.Handler; 
  10. import android.os.Message; 
  11. import android.view.LayoutInflater; 
  12. import android.view.View; 
  13. import android.view.ViewGroup; 
  14. import android.widget.AdapterView; 
  15. import android.widget.BaseAdapter; 
  16. import android.widget.LinearLayout; 
  17. import android.widget.ListView; 
  18. import android.widget.TextView; 
  19.  
  20. public class listButtonTest extends Activity { 
  21.     private ListView listView = null
  22.     private ListAdapter listAdapter = null
  23.  
  24.     /** Called when the activity is first created. */ 
  25.     @Override 
  26.     public void onCreate(Bundle savedInstanceState) { 
  27.         super.onCreate(savedInstanceState); 
  28.         setContentView(R.layout.main); 
  29.         listView = (ListView) this.findViewById(R.id.listView); 
  30.         ArrayList buttonListView = new ArrayList(); 
  31.         ButtonView a = new ButtonView(R.string.l1); 
  32.         buttonListView.add(a); 
  33.         ButtonView b = new ButtonView(R.string.l2); 
  34.         buttonListView.add(b); 
  35.         ButtonView c = new ButtonView(R.string.l3); 
  36.         buttonListView.add(c); 
  37.         ButtonView d = new ButtonView(R.string.l4); 
  38.         buttonListView.add(d); 
  39.         ButtonView e = new ButtonView(R.string.l5); 
  40.         buttonListView.add(e); 
  41.  
  42.         listAdapter = new ListAdapter(buttonListView); 
  43.         listView.setAdapter(listAdapter); 
  44.         listView.setDividerHeight(0); 
  45.  
  46.         listView.setOnItemClickListener(new ListView.OnItemClickListener() { 
  47.  
  48.             @Override 
  49.             public void onItemClick(AdapterView arg0, View arg1, int arg2, 
  50.                     long arg3) { 
  51.                 // TODO Auto-generated method stub 
  52.                 listAdapter.setSelectedPosition(arg2); 
  53.                   
  54.                 listAdapter.notifyDataSetInvalidated();    
  55.  
  56.             } 
  57.         }); 
  58.     }; 
  59.  
  60.   
  61.  
  62.     public class ListAdapter extends BaseAdapter { 
  63.  
  64.         ArrayList arrayList = null
  65.         LayoutInflater inflater; 
  66.         View view; 
  67.         ButtonLayoutHolder buttonLayoutHolder; 
  68.         LinearLayout buttonLayout = null
  69.         TextView buttonText = null
  70.  
  71.         private int selectedPosition = -1;// 选中的位置 
  72.  
  73.         public ListAdapter(ArrayList buttonListView) { 
  74.             // TODO Auto-generated constructor stub 
  75.             arrayList = buttonListView; 
  76.         } 
  77.  
  78.         @Override 
  79.         public int getCount() { 
  80.             // TODO Auto-generated method stub 
  81.             return arrayList.size(); 
  82.         } 
  83.  
  84.         @Override 
  85.         public Object getItem(int position) { 
  86.             // TODO Auto-generated method stub 
  87.             return arrayList.get(position); 
  88.         } 
  89.  
  90.         @Override 
  91.         public long getItemId(int position) { 
  92.             // TODO Auto-generated method stub 
  93.             return position; 
  94.         } 
  95.  
  96.         public void setSelectedPosition(int position) { 
  97.             selectedPosition = position; 
  98.         } 
  99.  
  100.         @Override 
  101.         public View getView(int position, View convertView, ViewGroup parent) { 
  102.             // TODO Auto-generated method stub 
  103.             inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  104.             view = inflater.inflate(R.layout.button_layout, null, false); 
  105.             buttonLayoutHolder = (ButtonLayoutHolder) view.getTag(); 
  106.  
  107.             if (buttonLayoutHolder == null) { 
  108.                 buttonLayoutHolder = new ButtonLayoutHolder(); 
  109.                 buttonLayoutHolder.buttonLayout = (LinearLayout) view 
  110.                         .findViewById(R.id.LinearLayoutButton); 
  111.                 buttonLayoutHolder.textView = (TextView) view 
  112.                         .findViewById(R.id.TextViewButton); 
  113.                 view.setTag(buttonLayoutHolder); 
  114.             } 
  115.             buttonLayout = buttonLayoutHolder.buttonLayout; 
  116.             buttonText = buttonLayoutHolder.textView; 
  117.             if (selectedPosition == position) { 
  118.                 buttonText.setSelected(true); 
  119.                 buttonText.setPressed(true); 
  120.                 buttonLayout.setBackgroundColor(Color.RED); 
  121.             } else
  122.                 buttonText.setSelected(false); 
  123.                 buttonText.setPressed(false); 
  124.                 buttonLayout.setBackgroundColor(Color.TRANSPARENT);    
  125.  
  126.             } 
  127.  
  128.             buttonText.setTextColor(Color.WHITE); 
  129.             buttonText.setText(arrayList.get(position).textViewId); 
  130.  
  131.             return view; 
  132.  
  133.         } 
  134.  
  135.     }; 
  136.  
  137.  
  138. class ButtonView { 
  139.     int textViewId; 
  140.  
  141.     ButtonView(int tId) { 
  142.         textViewId = tId; 
  143.     } 
  144.  
  145. class ButtonLayoutHolder { 
  146.     LinearLayout buttonLayout; 
  147.     TextView textView; 
package com.listButtonTest.www;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class listButtonTest extends Activity {
	private ListView listView = null;
	private ListAdapter listAdapter = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		listView = (ListView) this.findViewById(R.id.listView);
		ArrayList buttonListView = new ArrayList();
		ButtonView a = new ButtonView(R.string.l1);
		buttonListView.add(a);
		ButtonView b = new ButtonView(R.string.l2);
		buttonListView.add(b);
		ButtonView c = new ButtonView(R.string.l3);
		buttonListView.add(c);
		ButtonView d = new ButtonView(R.string.l4);
		buttonListView.add(d);
		ButtonView e = new ButtonView(R.string.l5);
		buttonListView.add(e);

		listAdapter = new ListAdapter(buttonListView);
		listView.setAdapter(listAdapter);
		listView.setDividerHeight(0);

		listView.setOnItemClickListener(new ListView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				listAdapter.setSelectedPosition(arg2);
				 
				listAdapter.notifyDataSetInvalidated();   

			}
		});
	};

 

	public class ListAdapter extends BaseAdapter {

		ArrayList arrayList = null;
		LayoutInflater inflater;
		View view;
		ButtonLayoutHolder buttonLayoutHolder;
		LinearLayout buttonLayout = null;
		TextView buttonText = null;

		private int selectedPosition = -1;// 选中的位置

		public ListAdapter(ArrayList buttonListView) {
			// TODO Auto-generated constructor stub
			arrayList = buttonListView;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return arrayList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return arrayList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public void setSelectedPosition(int position) {
			selectedPosition = position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.button_layout, null, false);
			buttonLayoutHolder = (ButtonLayoutHolder) view.getTag();

			if (buttonLayoutHolder == null) {
				buttonLayoutHolder = new ButtonLayoutHolder();
				buttonLayoutHolder.buttonLayout = (LinearLayout) view
						.findViewById(R.id.LinearLayoutButton);
				buttonLayoutHolder.textView = (TextView) view
						.findViewById(R.id.TextViewButton);
				view.setTag(buttonLayoutHolder);
			}
			buttonLayout = buttonLayoutHolder.buttonLayout;
			buttonText = buttonLayoutHolder.textView;
			if (selectedPosition == position) {
				buttonText.setSelected(true);
				buttonText.setPressed(true);
				buttonLayout.setBackgroundColor(Color.RED);
			} else {
				buttonText.setSelected(false);
				buttonText.setPressed(false);
				buttonLayout.setBackgroundColor(Color.TRANSPARENT);   

			}

			buttonText.setTextColor(Color.WHITE);
			buttonText.setText(arrayList.get(position).textViewId);

			return view;

		}

	};

}

class ButtonView {
	int textViewId;

	ButtonView(int tId) {
		textViewId = tId;
	}
}

class ButtonLayoutHolder {
	LinearLayout buttonLayout;
	TextView textView;
}

  在listView的setOnItemClickListener事件中标记这次选择的Item的下标:listAdapter.setSelectedPosition(arg2);

  然后调用listAdapter.notifyDataSetInvalidated()通知后台重新刷新界面。

  在ListAdapter的getView()方法中,如果是选中的Item则显示背景颜色,如果不是则不显示背景颜色。

你可能感兴趣的:(android中设置ListView的选中的Item的背景颜色)