java 设置listview单元格颜色_android中设置ListView的选中的Item的背景颜色

这篇博客介绍了如何在Android中为ListView设置选中项的背景颜色,使其在选择后依然保持高亮状态。主要步骤包括:1)在main.xml中配置ListView,设置listSelector为透明;2)在button_layout.xml中定义Item布局;3)在Activity中创建ListView,通过Adapter动态设置Item颜色。当点击Item时,通过listAdapter.setSelectedPosition()标记选中位置,并调用notifyDataSetInvalidated()刷新界面,从而实现选中项的背景颜色变化。
摘要由CSDN通过智能技术生成

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

1、配置main.xml

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

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

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

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">

android:id="@+id/TextViewButton"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:textSize="20px">

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 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则显示背景颜色,如果不是则不显示背景颜色。

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

【转】整理一下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中设置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 ...

随机推荐

JavaScript中的this

本文尽量避免概念性的讲解,那样太抽象 所以下面以一些简单的例子,从易到难对this的用法总结 1.方法中的this会指向当前执行该方法的对象 如: var name = "window&qu ...

Ubantu16.04一键部署Cacti监控服务器

Ubantu16.04一键部署Cacti监控服务器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 欢迎加入高级运维工程师之路:598432640 一.查看系统环境(关于该系统的安装 ...

切换到percona server各种问题

这两天把七八台服务器全部切换到了percona server,相关注意事项如下: 1.JDBC报ERROR 1862 (HY000): Your password has expired. To lo ...

搞ACM的你们伤不起

这个虽然看过很多遍了,但是还是看着想笑,有时候真的想问问自己为什么这么菜,血流得还不够? 劳资六年前开始搞ACM啊!!!!!!!!!!  从此踏上了尼玛不归路啊!!!!!!!!!!!!  谁特么跟劳资 ...

在VS Nuget命令行下进行EF数据库迁移

找到项目中,用到数据库DLL的地方,然后选中该项目,打开Nuget命令行输入以下的命令: 其中cardId为迁移名称,自己取

Vue和Bootstrap的整合之路

我是一个刚刚接触前端开发的新手,所以有必要记录如何将Bootstrap和Vue进行整合. 如果你是老手,请直接绕道而过.作为一个新手,里面的步骤,过程或者专业术语未必正确,如果你发现哪里错误了,请发邮 ...

linux下直接拷贝新版本R

如果要使用新版本的R,除了直接安装,也可以直接拷贝R的文件夹.这样既可以保留原始的R版本和R包,也可以使用新版本的R和R包,R包存放在R目录下的library文件夹. 文件放路径 R: /usr/ ...

WordPress中添加自定义评论表情包的方法

先来看看效果: 现在由于WordPress版本更新,再加上WordPress主题也越来越多,而现在的主题一般都是禁用了WordPress自带的评论表情,其实自带 的评论表情也是很丑的,但是以前我们可以 ...

2.SSM整合_多表_一对一或多对一的增删改查

一对一和多对一配置一样,这里就放到一起. 1.配置文件跟上一章一样,这里就不多写了,主要是Mapper映射文件 多 接口 public interface NewsMapper { public vo ...

mac下go环境搭建开发web工程

1,golang下载: http://www.golangtc.com/download https://golang.org/ https://beego.me/docs/intro/ 2,安装go

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值