ListView选中时,给item添加左边框

先看一下效果:

原理:把上一次选中的item对应的view缓存到oldDepartment中,当第二次点击时,如果点击的不是当前的item,那么去除oldDepartment的样式,给新选择的item添加样式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.example.mo.yiyuan;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* Created by MO on 2015/9/3 0003.
*/
public class Yuyue_mainactivity extends Activity {
ListView lvDepartment,lvSonDepartment;
private String[] lvDepartment_text = new String[]{
"外科",
"内科",
"五官",
"皮肤",
"儿科"
};
View oldDepartment;
int last_item = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yuyue_mainactivity);

lvDepartment = (ListView)findViewById(R.id.lvDepartment);
SimpleAdapter lvDepartmentAdapter = new SimpleAdapter(
this,getDepartmentData(),R.layout.department_item,new String[]{"lvDepartment_text"},new int[]{R.id.lvDepartment_text}
);
lvDepartment.setAdapter(lvDepartmentAdapter);
lvSonDepartment = (ListView) findViewById(R.id.lvSonDepartment);
lvDepartment.getChildAt(0).callOnClick();

lvDepartment.post(new Runnable() {//不放在Runnable中的话会报空指针异常
@Override
public void run() {
ImageView ivHead = (ImageView) lvDepartment.getChildAt(0).findViewById(R.id.ivHead);
ivHead.setVisibility(View.VISIBLE);
lvDepartment.getChildAt(0).setBackgroundColor(getResources().getColor(R.color.white));
List<Map<String, Object>> sonList = getSonDepartmentData(lvDepartment_text[0]);
SimpleAdapter lvSonDepartmentAdapter = new SimpleAdapter(
getApplication(), sonList, R.layout.sondepartment_item, new String[]{"son_text"}, new int[]{R.id.son_text}
);
lvSonDepartment.setAdapter(lvSonDepartmentAdapter);
oldDepartment = lvDepartment.getChildAt(0);
last_item = 0;
}
});


lvDepartment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

ImageView ivHead = (ImageView) view.findViewById(R.id.ivHead);
ivHead.setVisibility(View.VISIBLE);
view.setBackgroundColor(getResources().getColor(R.color.white));
List<Map<String, Object>> sonList = getSonDepartmentData(lvDepartment_text[position]);
SimpleAdapter lvSonDepartmentAdapter = new SimpleAdapter(
getApplication(), sonList, R.layout.sondepartment_item, new String[]{"son_text"}, new int[]{R.id.son_text}
);
lvSonDepartment.setAdapter(lvSonDepartmentAdapter);
if (last_item != -1 && last_item != position) {//点击的不是当前的item
oldDepartment.findViewById(R.id.ivHead).setVisibility(View.INVISIBLE);
oldDepartment.setBackgroundColor(getResources().getColor(R.color.transparent));

lvSonDepartmentAdapter.notifyDataSetChanged();
}
oldDepartment = view;
last_item = position;
}
});

lvSonDepartment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent it = new Intent(getApplicationContext(),Yuyue_SonDepartment.class);
startActivity(it);
}
});
}

private List<Map<String,Object>> getDepartmentData(){
List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
for (int i=0;i<lvDepartment_text.length;i++){
Map<String,Object> map = new HashMap<String, Object>();
map.put("lvDepartment_text",lvDepartment_text[i]);
list.add(map);
}
return list;
}

private List<Map<String,Object>> getSonDepartmentData(String str){
List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
for (int i=0;i<10;i++){
Map<String,Object> map = new HashMap<String, Object>();
map.put("son_text",str + i);
list.add(map);
}
return list;
}

}

注意:第一次打开界面时,也可以使用public boolean performItemClick (View view, int position, long id)来模拟Listview的item点击事件,具体使用方法:

1
2
3
4
5
6
mList.requestFocusFromTouch();
mList.setSelection(position);
mList.performItemClick(
mList.getAdapter().getView(mActivePosition, null, null),
mActivePosition,
mList.getAdapter().getItemId(mActivePosition));

遇到的一个问题是,在OnItemClickListener中,view.setBackgroundColor(Color.RED);更改背景色无效,而直接点击则正常,目前还不清楚原因,替代方案是使用mList.getChildAt(position).setBackgroundColor(Color.RED);

参考:
ListView 默认选中第X个高亮
http://stackoverflow.com/questions/8094268/android-listview-performitemclick
仿京东手机端类别页