Android開發(fā)listview選中高亮簡單實現(xiàn)代碼分享
百度了好幾種listview選中高亮的辦法都太繁瑣太不友好,我在無意中發(fā)現(xià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 = { "選項一", "選項二", "選項三", "選項四", "選項五" };
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é)
以上就是本文關于Android開發(fā)listview選中高亮簡單實現(xiàn)代碼分享的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
在android中ScrollView嵌套ScrollView解決方案
大家好,眾所周知,android里兩個相同方向的ScrollView是不能嵌套的,那要是有這樣的需求怎么辦,接下來為您介紹解決方法,感興趣的朋友可以了解下2013-01-01
Android實現(xiàn)整理PackageManager獲取所有安裝程序信息
這篇文章主要介紹了Android實現(xiàn)整理PackageManager獲取所有安裝程序信息的方法,實例分析了Android使用PackageManager獲取安裝程序信息的具體步驟與相關技巧,需要的朋友可以參考下2016-01-01
ubuntu環(huán)境下反編譯android apk的方法
今天小編就為大家分享一篇關于ubuntu環(huán)境下反編譯android apk的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Android 中使用ExpandableListView 實現(xiàn)分組的實例
這篇文章主要介紹了Android 中使用ExpandableListView 實現(xiàn)分組的實例的相關資料,這里提供實例代碼及實現(xiàn)效果圖,需要的朋友可以參考下2016-12-12
Android使用MediaRecorder類實現(xiàn)視頻和音頻錄制功能
Android提供了MediaRecorder這一個類來實現(xiàn)視頻和音頻的錄制功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧2018-07-07
Android中Webview打開網(wǎng)頁的同時發(fā)送HTTP頭信息方法
這篇文章主要介紹了Android中Webview打開網(wǎng)頁的同時發(fā)送HTTP頭信息方法,本文是講解的是一種通過修改Referer來控制盜鏈的方法,需要的朋友可以參考下2015-01-01
修改Android Studio 的 Logcat 緩沖區(qū)大小操作
這篇文章主要介紹了修改Android Studio 的 Logcat 緩沖區(qū)大小操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

