Android開發(fā)listview選中高亮簡(jiǎn)單實(shí)現(xiàn)代碼分享
百度了好幾種listview選中高亮的辦法都太繁瑣太不友好,我在無意中發(fā)現(xiàn)了一種簡(jiǎn)單有效的辦法,而且代碼量極少
源碼如下:
MainActivity.java
package com.listviewtest; import android.os.Bundle; import android.app.Activity; import android.graphics.drawable.Drawable; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity { private ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String[] items_text = { "選項(xiàng)一", "選項(xiàng)二", "選項(xiàng)三", "選項(xiàng)四", "選項(xiàng)五" }; listview = (ListView) findViewById(R.id.listView1); listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items_text)); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) { Drawable drawable=getResources().getDrawable(R.drawable.red); listview.setSelector(drawable); } } ); } }
activity_main.xml
<pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/listView1" android:background="@color/gray" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollingCache="false" /> </RelativeLayout>
values/strings.xml中添加
<pre name="code" class="html"> <drawable name="red">#ff0000</drawable>
總結(jié)
以上就是本文關(guān)于Android開發(fā)listview選中高亮簡(jiǎn)單實(shí)現(xiàn)代碼分享的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
android listview初步學(xué)習(xí)實(shí)例代碼
android listview進(jìn)階實(shí)例分享
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
- android listview進(jìn)階實(shí)例分享
- android listview初步學(xué)習(xí)實(shí)例代碼
- Android ListView實(shí)現(xiàn)下拉頂部圖片變大效果
- Android ListView與RecycleView的對(duì)比使用解析
- Android開發(fā)實(shí)現(xiàn)仿QQ消息SwipeMenuListView滑動(dòng)刪除置頂功能【附源碼下載】
- android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載
- android使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多
相關(guān)文章
Android簡(jiǎn)單自定義音樂波動(dòng)特效圖
這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)單自定義音樂波動(dòng)特效圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04在android中ScrollView嵌套ScrollView解決方案
大家好,眾所周知,android里兩個(gè)相同方向的ScrollView是不能嵌套的,那要是有這樣的需求怎么辦,接下來為您介紹解決方法,感興趣的朋友可以了解下2013-01-01Android實(shí)現(xiàn)整理PackageManager獲取所有安裝程序信息
這篇文章主要介紹了Android實(shí)現(xiàn)整理PackageManager獲取所有安裝程序信息的方法,實(shí)例分析了Android使用PackageManager獲取安裝程序信息的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-01-01ubuntu環(huán)境下反編譯android apk的方法
今天小編就為大家分享一篇關(guān)于ubuntu環(huán)境下反編譯android apk的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Android 中使用ExpandableListView 實(shí)現(xiàn)分組的實(shí)例
這篇文章主要介紹了Android 中使用ExpandableListView 實(shí)現(xiàn)分組的實(shí)例的相關(guān)資料,這里提供實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-12-12Android使用MediaRecorder類實(shí)現(xiàn)視頻和音頻錄制功能
Android提供了MediaRecorder這一個(gè)類來實(shí)現(xiàn)視頻和音頻的錄制功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07Android中如何實(shí)現(xiàn)清空搜索框的文字
本文主要介紹Android中實(shí)現(xiàn)清空搜索框的文字的方法。項(xiàng)目中的有關(guān)搜索的地方,加上清空文字的功能,目的是為了增加用戶體驗(yàn),使用戶刪除文本更加快捷。需要的朋友一起來看下吧2016-12-12Android中Webview打開網(wǎng)頁的同時(shí)發(fā)送HTTP頭信息方法
這篇文章主要介紹了Android中Webview打開網(wǎng)頁的同時(shí)發(fā)送HTTP頭信息方法,本文是講解的是一種通過修改Referer來控制盜鏈的方法,需要的朋友可以參考下2015-01-01修改Android Studio 的 Logcat 緩沖區(qū)大小操作
這篇文章主要介紹了修改Android Studio 的 Logcat 緩沖區(qū)大小操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04