Android ListView的item背景色设置和item点击无响应的解决方法
时间:2021-07-30 11:26:16
下面讲解以下在使用listview时最常见的几个问题。
1.如何改变item的背景色和按下颜色
listview默认情况下,item的背景色是黑色,在用户点击时是黄色的。如果需要修改为自定义的背景颜色,一般情况下有三种方法:
1)设置listSelector
2)在布局文件中设置item的background
3)在adapter的getview中设置
这三种方法都能达到改变item默认的背景色和按下颜色,下面来分别讲解,但是在这之前需要先写好selector.xml文件;
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/light_blue"></item>
<item android:state_pressed="false" android:drawable="@color/sgray"></item>
</selector>
在改变button或者listview的item默认背景色,就可以用到selector。drawable可以设置为色彩资源,也可以设置为图片资源。
1)设置listview的listSelector
<ListView
android:id="@+id/history_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#565C5D"
android:dividerHeight="3dp"
android:listSelector="@drawable/selector"
android:cacheColorHint="@android:color/transparent">
</ListView>
2)在listitem的布局文件中设置background属性,下面是listitem的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/selector">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="历史记录"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_centerInParent="true">
</TextView>
</RelativeLayout>
3)在adapter的getView方法中设置
if(convertView ==null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.listitem, null);
}
convertView.setBackgroundResource(R.drawable.selector);
上述方法都能达到同样的效果,就是改变item默认的背景色和点击时的背景颜色,第三种方法最灵活,如果listview的奇数行和偶数行需要设置为不同的selector,只能用第三种方法。
2.包含button,checkbox等控件时点击无响应问题。
如果listitem里面包括button或者checkbox等控件,默认情况下listitem会失去焦点,导致无法响应item的事件,最常用的解决办法是在listitem的布局文件中设置descendantFocusability属性。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="@+id/history_item_checkbt"
android:layout_height="30dp"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:checked="false"
>
</CheckBox>
<ImageView
android:id="@+id/history_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/history_item_checkbt"
android:background="@drawable/item_icon">
</ImageView>
<Button
android:id="@+id/history_item_edit_bt"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="编辑"
android:textColor="#ffffff"
android:textSize="14sp"
android:background="@drawable/button_bg">
</Button>
<TextView
android:id="@+id/history_item_time_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#565C5D"
android:textSize="14sp"
android:text="10-01 10:20"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@id/history_item_edit_bt">
</TextView>
<TextView
android:id="@+id/history_item_title_tv"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_centerVertical="true"
android:textColor="#565C5D"
android:textSize="14sp"
android:text="xxxxxxxxXXXXXXXXXXXXXXXX"
android:ellipsize="end"
android:maxLines="1"
android:layout_toRightOf="@id/history_item_image"
android:layout_toLeftOf="@id/history_item_time_tv"
android:layout_marginLeft="3dp">
</TextView>
</RelativeLayout>
标签:Android,ListView
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
秒懂Java枚举类型(enum)
2023-03-30 07:39:41
Spring Security内置过滤器的维护方法
2022-07-30 18:10:16
![](https://img.aspxhome.com/file/2023/5/61285_0s.jpg)
Java创建线程池为什么一定要用ThreadPoolExecutor
2023-04-22 06:03:31
![](https://img.aspxhome.com/file/2023/1/64391_0s.webp)
简单聊一聊Java线程池ThreadPoolExecutor
2021-10-23 15:56:58
![](https://img.aspxhome.com/file/2023/3/64053_0s.jpg)
mybatis in查询条件过长的解决方案
2022-06-08 12:44:14
![](https://img.aspxhome.com/file/2023/4/70594_0s.png)
SpringBoot实现Mysql使用MD5进行密码加密的示例
2022-11-24 23:14:16
![](https://img.aspxhome.com/file/2023/6/62156_0s.png)
java中final与finally的使用介绍
2023-04-21 05:36:43
C#加密在实际中的应用
2022-05-11 14:57:21
![](https://img.aspxhome.com/file/2023/2/70432_0s.jpg)
Gradle进阶使用结合Sonarqube进行代码审查的方法
2021-12-17 08:49:56
![](https://img.aspxhome.com/file/2023/6/84226_0s.png)
java IO流将一个文件拆分为多个子文件代码示例
2023-08-30 12:46:15
![](https://img.aspxhome.com/file/2023/3/58193_0s.jpg)
c#项目将dll打包到exe中的步骤
2021-06-21 14:44:25
windows下jar包开机自动重启的步骤
2022-10-17 22:21:41
详解Java单元测试之Junit框架使用教程
2022-03-06 02:55:56
![](https://img.aspxhome.com/file/2023/8/61788_0s.png)
C# KeyUp事件中MessageBox的回车(Enter)键回调问题解决方案
2021-06-25 06:24:06
Java 字符串转float运算 float转字符串的方法
2022-04-09 10:09:06
基于FLink实现实时安全检测的示例代码
2022-05-06 11:03:11
详解Java实现单例的五种方式
2021-06-11 03:54:03
Java SpringMVC异步处理详解
2021-08-10 15:03:58
![](https://img.aspxhome.com/file/2023/0/62050_0s.png)
MyBatisPlus 大数据量查询慢的问题解决
2022-06-17 05:56:39
解决Mybatis 大数据量的批量insert问题
2023-09-08 13:59:40
![](https://img.aspxhome.com/file/2023/0/88080_0s.jpg)