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

1、配置main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <ListView android:id="@+id/listView" android:listSelector="#000000"
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content" />
  8. </LinearLayout>

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@+id/LinearLayoutButton"
  5. android:layout_width="144px"
  6. android:layout_height="99px"
  7. android:gravity="center"
  8. android:orientation="vertical">
  9. <TextView
  10. android:id="@+id/TextViewButton"
  11. android:layout_width="fill_parent"
  12. android:layout_height="fill_parent"
  13. android:textSize="20px">
  14. </TextView>
  15. </LinearLayout>

3、实现Activity

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

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

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

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

http://longyi-java.iteye.com/blog/976067

android中设置ListView的选中的Item的背景颜色的更多相关文章

  1. 【转】整理一下Android中的ListView

    原文网址:http://sunbofu.blog.51cto.com/6431507/1280441 Android中的listview目测是一个使用频率很高的组件,所以今天来总结一下listview ...

  2. Android中的ListView属性使用总结

    Android中使用ListView控件比较常见,如果能知道常用的一些属性使用,肯定会少很多坑. 1.ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景 ...

  3. 【转】Android中设置TextView的颜色setTextColor--代码中设置字体颜色

    原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html android中设置TextView的颜色有方法setText ...

  4. 【转】Android中设置TextView的颜色setTextColor

    原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html android中设置TextView的颜色有方法setText ...

  5. Android中使用ListView绘制自定义表格(2)

    上回再写了<Android中使用ListView绘制自定义表格>后,很多人留言代码不全和没有数据样例.但因为项目原因,没法把源码全部贴上来.近两天,抽空简化了一下,做了一个例子. 效果图如 ...

  6. [转]Android中设置TextView的颜色setTextColor

    [转自]http://txlong-onz.iteye.com/blog/1249609 Android中设置TextView的颜色setTextColor android中设置TextView的颜色 ...

  7. Android中实现ListView圆角效果[转]

    本文演示如何Android中实现ListView圆角效果. 无论是网站,还是APP,人们都爱看一些新颖的视图效果.直角看多了,就想看看圆角,这几年刮起了一阵阵的圆角设计风:CSS新标准纳入圆角元素,特 ...

  8. 在Eclipse Android中设置模拟器屏幕大小

    在Eclipse Android中设置模拟器屏幕大小是本文要介绍的内容,主要是来了解并学习Eclipse Android中模拟器的设置,具体关于Eclipse Android内容的详解来看本文. 方法 ...

  9. Android中设置TextView的颜色setTextColor

    tv.setTextColor(Color.parseColor("#FFFFFF")); tv.setTextColor(Color.WHITE); tv.setTextColo ...

随机推荐

  1. H5的新应用-拖到页面上的元素

    -------------------------- <script type="text/javascript">                        // ...

  2. scala与java的==的比较

    如果你想比较一下看看两个对象是否相等,可以使用或者==,或它的反义 !=.(对所有对象都适用,而不仅仅是基本数据类型) ? 1 2 3 4 scala> 1 ==  2 res24: Boole ...

  3. nefu 899这也是裸的找

    #include <iostream> #include <algorithm> #include <cstdio> using namespace std; in ...

  4. Django:之不得不说的web框架们

    python的web框架 Bottle Bpttle是一个快速.简洁.轻量级的基于WSIG的微型web框架,此框架只有一个.py文件,除了python的标准库外,其不依赖任何其它模块. pip ins ...

  5. ecshop订单状态对应值详解

    ecshop的订单状态都是在ecs_order_info表中的字段里. 订单状态 未确认 取消 确认 已付款 配货中 已发货 已收货 退货 order_status 0 2 1 1 1 5 5 4 s ...

  6. Array.length vs Array.prototype.length

    I found that both the Array Object and Array.prototype have the length property. I am confused on us ...

  7. 学习笔记:GLSL Core Tutorial – Pipeline (OpenGL 3.2 – OpenGL 4.2)

    GLSL Core Tutorial – Pipeline (OpenGL 3.2 – OpenGL 4.2) GLSL 是一种管道,一种图形化的流水线 1.GLSL 的具体工作流程: 简化流程如下: ...

  8. Outing

    Outing 题目描述 Organising a group trip for the elderly can be a daunting task... Not least because of t ...

  9. SELinux(Security-Enhanced Linux)

    http://blog.csdn.net/myarrow/article/details/9839377 Security-Enhanced Linux(SELinux)的历史 一个小历史将有助于帮助 ...

  10. json的学习笔记

    json比较简单,所以先从json开始学起. 一 json的名称: json的全称是javascript object notation,中文名称为js 对象表示法. json的定义:json是一种轻 ...