Android開發(fā)listview選中高亮簡單實(shí)現(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 = { "選項(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選中高亮簡單實(shí)現(xiàn)代碼分享的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
android listview初步學(xué)習(xí)實(shí)例代碼
android listview進(jìn)階實(shí)例分享
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
- android listview進(jìn)階實(shí)例分享
- android listview初步學(xué)習(xí)實(shí)例代碼
- Android ListView實(shí)現(xiàn)下拉頂部圖片變大效果
- Android ListView與RecycleView的對比使用解析
- Android開發(fā)實(shí)現(xiàn)仿QQ消息SwipeMenuListView滑動(dòng)刪除置頂功能【附源碼下載】
- android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載
- android使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多
相關(guān)文章
Android 2d游戲開發(fā)之貪吃蛇基于surfaceview
這篇文章主要介紹了Android 2d游戲開發(fā)基于surfaceview的貪吃蛇,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
基于Android實(shí)現(xiàn)系統(tǒng)重啟reboot功能
在某些特殊場景下(如設(shè)備管理、安全監(jiān)控、工控系統(tǒng)等),開發(fā)者可能需要實(shí)現(xiàn)系統(tǒng)重啟功能,本文給大家介紹了如何基于Android實(shí)現(xiàn)系統(tǒng)重啟reboot功能,需要的朋友可以參考下2025-04-04
Android畢業(yè)設(shè)計(jì)備忘錄APP
這篇文章主要介紹了一個(gè)Android畢業(yè)設(shè)計(jì)備忘錄APP,它很小,但是功能很全,可實(shí)現(xiàn)添加、刪除、修改、查看的功能,使用Java語言開發(fā),風(fēng)格簡練2021-08-08
Android自定義扇形倒計(jì)時(shí)實(shí)例代碼
最近工作中需要做一個(gè)倒計(jì)時(shí),是那種一個(gè)圓,慢慢的被吃掉的動(dòng)畫倒計(jì)時(shí),由于自己是android小白,效果還不是多滿意,先給大家分享實(shí)例代碼,僅供大家參考2017-03-03
Android StepView實(shí)現(xiàn)物流進(jìn)度效果
這篇文章主要為大家詳細(xì)介紹了Android StepView實(shí)現(xiàn)物流進(jìn)度效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android RecyclerView仿新聞?lì)^條的頻道管理功能
這篇文章主要介紹了Android RecyclerView仿新聞?lì)^條的頻道管理功能,需要的朋友可以參考下2017-06-06
Android中控件GridView實(shí)現(xiàn)設(shè)置行列分割線的方法示例
這篇文章主要介紹了利用Android中控件GridView實(shí)現(xiàn)設(shè)置行列分割線的方法,文中給出了詳細(xì)的介紹與示例代碼,相信對大家具有一定的參考價(jià)值,有需要的朋友們下面來一起看看吧。2017-01-01

