首页 > 代码库 > android中设置ListView的选中的Item的背景颜色
android中设置ListView的选中的Item的背景颜色
ListView中没有默认的选择颜色,只有选择Item后的焦点颜色,鼠标点击时Item有颜色,放开鼠标后颜色也就没有了,要实现放开鼠标后选择项的背景还是有颜色的。
1、配置main.xml
Java代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ListView android:id="@+id/listView" android:listSelector="#000000"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
设置Item的获取焦点颜色为白色android:listSelector="#000000"(即不显示背景颜色)
2、配置用于ListView显示Item的button_layout.xml
Java代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/LinearLayoutButton"
- android:layout_width="144px"
- android:layout_height="99px"
- android:gravity="center"
- android:orientation="vertical">
- <TextView
- android:id="@+id/TextViewButton"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:textSize="20px">
- </TextView>
- </LinearLayout>
3、实现Activity
Java代码
- 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<ButtonView> buttonListView = new ArrayList<ButtonView>();
- 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<ButtonView> arrayList = null;
- LayoutInflater inflater;
- View view;
- ButtonLayoutHolder buttonLayoutHolder;
- LinearLayout buttonLayout = null;
- TextView buttonText = null;
- private int selectedPosition = -1;// 选中的位置
- public ListAdapter(ArrayList<ButtonView> 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则显示背景颜色,如果不是则不显示背景颜色。
http://longyi-java.iteye.com/blog/976067
android中设置ListView的选中的Item的背景颜色
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。