listview 選中高亮顯示實(shí)現(xiàn)方法

剛開(kāi)始實(shí)現(xiàn)的時(shí)候,我打算使用ListView的 getChildAt(int
pos)方法來(lái)實(shí)現(xiàn),結(jié)果發(fā)現(xiàn)非常的cao蛋,因?yàn)長(zhǎng)istView本身的原因,當(dāng)你View
view=listView.getChildAt(pos),并且改變這個(gè)View的狀態(tài)時(shí),你會(huì)發(fā)現(xiàn),高亮的往往不是你
選中的那行,反而是其他行,這是由于ListView本身決定了,Google在設(shè)計(jì)ListView的時(shí)候,為了減少內(nèi)存的消耗,使 用了一種共用的方式,即多個(gè)行共用一個(gè)View,所以才會(huì)出現(xiàn)點(diǎn)擊后亂跳的現(xiàn)象(這是一種很高明的方法,不過(guò)也苦了程序猿們);
于是就選擇了另外一種方式,ListView都有一個(gè)Adapter來(lái)顯示數(shù)據(jù),而這個(gè)Adapter中的getView()卻能獲得準(zhǔn)確的每一行,這里我們需要自定義一個(gè)繼承自BaseAdapter的Adapter來(lái)實(shí)現(xiàn)(如果使用ArrayAdapter等自帶的Adapter時(shí),不會(huì)實(shí)現(xiàn)這樣的功能);另外,這個(gè)ListView一定要設(shè)置一個(gè)屬性:listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
即設(shè)置為單選模式,這樣當(dāng)你點(diǎn)擊一行后,ListView就會(huì)刷新界面,還有就是給ListView設(shè)置OnItemClickListener監(jiān)聽(tīng)器, 當(dāng)點(diǎn)擊一行后,就更新當(dāng)前行的下標(biāo),所有代碼如下所示:
public class ListViewDemo extends Activity {
private ListView listview;
private int cur_pos = 0;// 當(dāng)前顯示的一行
private String[] items_text = { "選項(xiàng)一", "選項(xiàng)二", "選項(xiàng)三", "選項(xiàng)四", "選項(xiàng)五" };
private int[] items_img = { R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_demo);
listview = (ListView) findViewById(R.id.listview);
final MyAdapter adapter = new MyAdapter(this);
listview.setAdapter(adapter);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);// 一定要設(shè)置這個(gè)屬性,否則ListView不會(huì)刷新
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
cur_pos = position;// 更新當(dāng)前行
}
});
}
private class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
public MyAdapter(Context context) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items_text.length;
}
@Override
public Object getItem(int position) {
return items_text[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.e("TEST", "refresh once");
convertView = inflater.inflate(R.layout.list_child, null, false);
ImageView img = (ImageView) convertView
.findViewById(R.id.list_child_img);// 用于顯示圖片
TextView tv = (TextView) convertView
.findViewById(R.id.list_child_text);// 顯示文字
tv.setText(items_text[position]);
img.setImageResource(items_img[position]);
if (position == cur_pos) {// 如果當(dāng)前的行就是ListView中選中的一行,就更改顯示樣式
convertView.setBackgroundColor(Color.LTGRAY);// 更改整行的背景色
tv.setTextColor(Color.RED);// 更改字體顏色
}
return convertView;
}
}
}
public class ListViewDemo extends Activity {
private ListView listview;
private int cur_pos = 0;// 當(dāng)前顯示的一行
private String[] items_text = { "選項(xiàng)一", "選項(xiàng)二", "選項(xiàng)三", "選項(xiàng)四", "選項(xiàng)五" };
private int[] items_img = { R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_demo);
listview = (ListView) findViewById(R.id.listview);
final MyAdapter adapter = new MyAdapter(this);
listview.setAdapter(adapter);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);// 一定要設(shè)置這個(gè)屬性,否則ListView不會(huì)刷新
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
cur_pos = position;// 更新當(dāng)前行
}
});
}
private class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
public MyAdapter(Context context) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items_text.length;
}
@Override
public Object getItem(int position) {
return items_text[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.e("TEST", "refresh once");
convertView = inflater.inflate(R.layout.list_child, null, false);
ImageView img = (ImageView) convertView
.findViewById(R.id.list_child_img);// 用于顯示圖片
TextView tv = (TextView) convertView
.findViewById(R.id.list_child_text);// 顯示文字
tv.setText(items_text[position]);
img.setImageResource(items_img[position]);
if (position == cur_pos) {// 如果當(dāng)前的行就是ListView中選中的一行,就更改顯示樣式
convertView.setBackgroundColor(Color.LTGRAY);// 更改整行的背景色
tv.setTextColor(Color.RED);// 更改字體顏色
}
return convertView;
}
}
}
相關(guān)文章
android仿即刻點(diǎn)贊文字部分的自定義View的示例代碼
本篇文章主要介紹了android仿即刻點(diǎn)贊文字部分的自定義View的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android應(yīng)用中使用ListView來(lái)分頁(yè)顯示刷新的內(nèi)容
這篇文章主要介紹了Android應(yīng)用中使用ListView來(lái)分頁(yè)顯示刷新的內(nèi)容的方法,展示了一個(gè)點(diǎn)擊按鈕進(jìn)行刷新的實(shí)例以及下拉刷新分頁(yè)顯示的要點(diǎn)解析,需要的朋友可以參考下2016-04-04Android使用URLConnection提交請(qǐng)求的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android使用URLConnection提交請(qǐng)求的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10RecyclerView中監(jiān)聽(tīng)EditText變化的BUG的解決方法
本篇文章主要介紹了RecyclerView中監(jiān)聽(tīng)EditText變化的BUG的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11如何使用Flutter實(shí)現(xiàn)58同城中的加載動(dòng)畫(huà)詳解
這篇文章主要給大家介紹了關(guān)于如何使用Flutter實(shí)現(xiàn)58同城中加載動(dòng)畫(huà)詳?shù)南嚓P(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10android實(shí)現(xiàn)NFC讀寫(xiě)功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)NFC讀寫(xiě)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09SimpleCommand實(shí)現(xiàn)圖片下載(二)
這篇文章主要為大家詳細(xì)介紹了SimpleCommand實(shí)現(xiàn)圖片下載,并顯示到ImageView控件上,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android程序報(bào)錯(cuò)程序包org.apache.http不存在問(wèn)題的解決方法
這篇文章主要介紹了Android程序報(bào)錯(cuò)"程序包org.apache.http不存在——Android 6.0已經(jīng)不支持HttpClient" 問(wèn)題的解決方法,感興趣的小伙伴們可以參考一下2016-06-06