android中设置ListView的选中的Item的背景颜色
ListView中没有默认的选择颜色,只有选择Item后的焦点颜色,鼠标点击时Item有颜色,放开鼠标后颜色也就没有了,要实现放开鼠标后选择项的背景还是有颜色的。
1、配置main.xml
- <?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
- <?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
- 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的背景颜色的更多相关文章
- 【转】整理一下Android中的ListView
原文网址:http://sunbofu.blog.51cto.com/6431507/1280441 Android中的listview目测是一个使用频率很高的组件,所以今天来总结一下listview ...
- Android中的ListView属性使用总结
Android中使用ListView控件比较常见,如果能知道常用的一些属性使用,肯定会少很多坑. 1.ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景 ...
- 【转】Android中设置TextView的颜色setTextColor--代码中设置字体颜色
原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html android中设置TextView的颜色有方法setText ...
- 【转】Android中设置TextView的颜色setTextColor
原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html android中设置TextView的颜色有方法setText ...
- Android中使用ListView绘制自定义表格(2)
上回再写了<Android中使用ListView绘制自定义表格>后,很多人留言代码不全和没有数据样例.但因为项目原因,没法把源码全部贴上来.近两天,抽空简化了一下,做了一个例子. 效果图如 ...
- [转]Android中设置TextView的颜色setTextColor
[转自]http://txlong-onz.iteye.com/blog/1249609 Android中设置TextView的颜色setTextColor android中设置TextView的颜色 ...
- Android中实现ListView圆角效果[转]
本文演示如何Android中实现ListView圆角效果. 无论是网站,还是APP,人们都爱看一些新颖的视图效果.直角看多了,就想看看圆角,这几年刮起了一阵阵的圆角设计风:CSS新标准纳入圆角元素,特 ...
- 在Eclipse Android中设置模拟器屏幕大小
在Eclipse Android中设置模拟器屏幕大小是本文要介绍的内容,主要是来了解并学习Eclipse Android中模拟器的设置,具体关于Eclipse Android内容的详解来看本文. 方法 ...
- Android中设置TextView的颜色setTextColor
tv.setTextColor(Color.parseColor("#FFFFFF")); tv.setTextColor(Color.WHITE); tv.setTextColo ...
随机推荐
- 其他应用和技巧-用Json格式来保存数据
-------------------- <script type="text/javascript"> //定义json变量 ...
- LeetCode OJ 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- oc汉子转拼音
oc中可以不使用第三方库直接吧数组转成拼音: 代码如下: NSString *str = @"中国abc人民共和国"; CFStringRef aCFString=(__bridg ...
- EF中用Newtonsoft.Json引发的循环引用问题
描述: 1.双向关系表a->b b->aList 2.在查询a引用b以后 3.用Newtonsoft.Json 去tojsonstring 4.一个只有6条数据的json串 出现了一屏幕字 ...
- 【转载】CentOS 6.4下PXE+Kickstart无人值守安装操作系统
[转载]CentOS 6.4下PXE+Kickstart无人值守安装操作系统 转自:CentOS 6.4下PXE+Kickstart无人值守安装操作系统 - David_Tang - 博客园 http ...
- Hibernate创建SessionFactory实例
private static SessionFactory sessionFactory = null; static { Configuration configuration =new Con ...
- docker certificates
x509: certificate is valid for mmtrkjy.com, *.mmtrkjy.com, *.mmtrkmc.com, *.mmtrkpd.com, *.mmtrksg.c ...
- PHP全选择删除功能
<script type="text/javascript" language="javascript"> function selectBox(s ...
- 【转】ethtool 命令详解
命令描述: ethtool 是用于查询及设置网卡参数的命令. 使用概要:ethtool ethx //查询ethx网口基本设置,其中 x 是对应网卡的编号,如eth0.eth1等等etht ...
- FTP: Configuring server users..
4 points to create a user to uploade to ftproot.. this user must be an administrator, and be able to ...