search
HomeJavajavaTutorialAndroid ListView item background color setting and item click unresponsive solution

  下面讲解以下在使用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的item背景色设置和item点击无响应的解决方法相关文章请关注PHP中文网!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, SvelteTop 4 JavaScript Frameworks in 2025: React, Angular, Vue, SvelteMar 07, 2025 pm 06:09 PM

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue FixedSpring Boot SnakeYAML 2.0 CVE-2022-1471 Issue FixedMar 07, 2025 pm 05:52 PM

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

Node.js 20: Key Performance Boosts and New FeaturesNode.js 20: Key Performance Boosts and New FeaturesMar 07, 2025 pm 06:12 PM

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How to Share Data Between Steps in CucumberHow to Share Data Between Steps in CucumberMar 07, 2025 pm 05:55 PM

This article explores methods for sharing data between Cucumber steps, comparing scenario context, global variables, argument passing, and data structures. It emphasizes best practices for maintainability, including concise context use, descriptive

Iceberg: The Future of Data Lake TablesIceberg: The Future of Data Lake TablesMar 07, 2025 pm 06:31 PM

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

How can I implement functional programming techniques in Java?How can I implement functional programming techniques in Java?Mar 11, 2025 pm 05:51 PM

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment