android listview改变选中项为高亮

来源:互联网 发布:java前后端数据交互 编辑:程序博客网 时间:2024/04/30 07:25

项目中需要实现对listview选中项保持高亮,在网上找到了一篇文章,参照实现了该功能。

转载地址:http://www.cnblogs.com/zcdqs/archive/2011/10/23/2221935.html

转载部分内容如下:

这里提供另一种保持高亮的方法:

在包含ListView的布局文件中加入一个空的LinearLayout,以我的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">

<LinearLayout android:layout_width="fill_parent"

android:layout_height="wrap_content" android:focusableInTouchMode="true">

</LinearLayout>

<ListView android:id="@+id/listView" android:layout_width="fill_parent"

android:layout_height="wrap_content"></ListView>

</LinearLayout>

然后在onItemClick中加入

listView.clearFocus();

listView.requestFocusFromTouch();

其中listView是你用findViewById获得的ListView

这样就可以了,具体原理未知。

还希望有达人能解惑。

原创粉丝点击