MyAdapter.java
1 package com.testview;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import android.content.Context;
7 import android.graphics.Color;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.BaseAdapter;
12 import android.widget.TextView;
13
14 public class MyAdapter extends BaseAdapter {
15 private LayoutInflater mInflater;
16 private List<Map<String, Object>>mData ;
17 private int selectItem=-1;
18 private int[] layout;
19 public MyAdapter(Context context, List<Map<String, Object>>mData, int[] layout) {
20 this.mInflater = LayoutInflater.from(context);
21 this.mData = mData;
22 this.layout = layout;
23 }
24 public int getCount() {
25 return mData.size();
26 }
27 public Object getItem(int arg0) {
28 return mData.get(arg0);
29 }
30 public long getItemId(int arg0) {
31 return arg0;
32 }
33 public View getView(int position, View convertView, ViewGroup parent) {
34 ViewHolder holder = null;
35 if (convertView == null) {
36 holder = new ViewHolder();
37 convertView = mInflater.inflate(layout[0], null);
38 holder.titleText = (TextView) convertView.findViewById(layout[1]);
39 holder.infoText = (TextView) convertView.findViewById(layout[2]);
40
41 convertView.setTag(holder);
42 } else {
43 holder = (ViewHolder) convertView.getTag();
44 }
45 holder.titleText.setText((String) mData.get(position).get("title"));
46 holder.infoText.setText((String) mData.get(position).get("info"));
47
48 if (position == selectItem) {
49 convertView.setBackgroundColor(0xaeefcc00);
50 }
51 else {
52 convertView.setBackgroundColor(Color.BLACK);
53 }
54 convertView.getBackground().setAlpha(20);//0~255透明度值
55 return convertView;
56 }
57 public void setSelectItem(int selectItem) {
58 this.selectItem = selectItem;
59 }
60
61 }
TestListViewActivity .java
1 package com.testview;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9 import com.util.SDUtil;
10 import android.app.Activity;
11 import android.app.AlertDialog;
12 import android.os.Bundle;
13 import android.os.Environment;
14 import android.view.KeyEvent;
15 import android.view.View;
16 import android.view.View.OnClickListener;
17 import android.widget.AdapterView;
18 import android.widget.EditText;
19 import android.widget.ImageButton;
20 import android.widget.ListView;
21
22 public class TestListViewActivity extends Activity {
23 /**
24 * 数据存储
25 */
26 private List<Map<String, Object>> mData;
27
28 /**
29 * 当前访问路径
30 */
31 private String currentPath = "/sdcard";
32
33 private ListView setlistViewLeft;
34 /**
35 * 自定义数据适配器
36 */
37 private MyAdapter adapter;
38
39 /**
40 * 文件名称
41 */
42 private String[] fileNames = null;
43
44 /**
45 * 文件路径
46 */
47 private String[] filePaths = null;
48
49 private EditText editText;
50 private ImageButton imageBtn;
51
52 private int[] layout = {
53 R.layout.listinfo,
54 R.id.titleleftlist,
55 R.id.infoleftlist
56 };
57
58 @Override
59 public void onCreate(Bundle savedInstanceState) {
60 super.onCreate(savedInstanceState);
61 setContentView(R.layout.main);
62 setTitle("文件总数:"+getSDCardMassage(currentPath)+"\n"+"当前路径:"+currentPath);
63 getSearchKey();
64 mData = getListItems();
65 setlistViewLeft = (ListView)findViewById(R.id.listleft);
66 adapter = new MyAdapter(this, mData, layout);
67 setlistViewLeft.setAdapter(adapter);
68 setlistViewLeft.setOnItemClickListener(mLeftListOnItemClick);
69 }
70
71 /**
72 * 初始化数据
73 */
74 public void getSearchKey(){
75 editText = (EditText) findViewById(R.id.entry);
76 imageBtn = (ImageButton) findViewById(R.id.btn_imageButton);
77 imageBtn.setOnClickListener(new OnClickListener() {
78
79 @Override
80 public void onClick(View v) {
81 String searchKey = editText.getText().toString();
82 if(searchKey == null || "".equals(searchKey)){
83 return;
84 }
85 int length = fileNames.length;
86 String[] strKeyName = {};
87 String[] strKeyPath = {};
88 for(int i = 0; i < length; i++){
89 String name = fileNames[i];
90 if(name.contains(searchKey)){
91 strKeyName = Arrays.copyOf(strKeyName, strKeyName.length+1);
92 strKeyPath = Arrays.copyOf(strKeyPath, strKeyPath.length+1);
93 strKeyName[strKeyName.length-1] = name;
94 strKeyPath[strKeyPath.length-1] = filePaths[i];
95 }
96 }
97 if(strKeyName.length > 0){
98 //初始化数据
99 setSearchKeyData(strKeyName, strKeyPath);
100
101 }else{
102 new AlertDialog.Builder(TestListViewActivity.this)
103 .setTitle("消息")
104 .setMessage("文件不存在")
105 .setPositiveButton("确定", null)
106 .show();
107 }
108 }
109
110
111 });
112 }
113
114 /**
115 * 初始化搜索数据
116 * @param strKeyName
117 * @param strKeyPath
118 */
119 private void setSearchKeyData(String[] strKeyName, String[] strKeyPath) {
120 fileNames = strKeyName;
121 filePaths = strKeyPath;
122 mData = getListItems();
123 setTitle("文件总数:"+strKeyName.length+"\n"+"当前路径:"+currentPath);
124 initAdapter();
125 }
126
127 /**
128 * 初始化数据
129 * @param path 文件路径
130 */
131 public void initData(String path){
132 currentPath = path;
133 setTitle("文件总数:"+getSDCardMassage(path)+" "+currentPath);
134 mData = getListItems();
135 editText.setText(null);
136 initAdapter();
137 }
138
139 public void initAdapter(){
140 setlistViewLeft = (ListView)findViewById(R.id.listleft);
141 adapter = new MyAdapter(this, mData, layout);
142 setlistViewLeft.setAdapter(adapter);
143 setlistViewLeft.setOnItemClickListener(mLeftListOnItemClick);
144 }
145
146 private List<Map<String,Object>> getListItems(){
147 List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
148 for(int i=0;i<fileNames.length;i++){
149 Map<String,Object> map = new HashMap<String, Object>();
150 map.put("title", "文件名称");
151 map.put("info", fileNames[i]);
152 listItems.add(map);
153 }
154 return listItems;
155 }
156
157 AdapterView.OnItemClickListener mLeftListOnItemClick = new AdapterView.OnItemClickListener() {
158
159 @Override
160 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
161 long arg3) {
162
163 adapter.setSelectItem(arg2);
164 adapter.notifyDataSetInvalidated();
165 String v2 = mData.get(arg2).get("info").toString();
166 String str = filePaths[arg2];
167 if(str != null){
168 //重新初始化界面
169 mData.clear();
170 currentPath = str;
171 initData(str);
172 }else{
173 SDUtil.openFile(TestListViewActivity.this, currentPath+"/"+v2);
174 }
175
176 }
177
178 };
179 /**
180 * 重写手机返回键事件
181 */
182 public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
183 if(keyCode == KeyEvent.KEYCODE_BACK ){
184 back();
185 return true;
186 }
187 return super.onKeyDown(keyCode, event);
188 };
189
190 /**
191 * 返回数据初始化
192 */
193 private void back() {
194 String path = new File(currentPath).getParentFile().getPath();
195 if("/sdcard".equals(currentPath)){
196 finish();
197 return;
198 }
199 initData(path);
200 }
201
202 /**
203 * 获取SD卡文件
204 * @param path
205 * @return
206 */
207 public int getSDCardMassage(String path){
208 boolean flag = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
209 if(flag){
210 if(SDUtil.isExistFiles(path)){
211 File f = new File(path);
212 File[] files = f.listFiles();
213 int size = files.length;
214 fileNames = new String[size];
215 filePaths = new String[size];
216 for(int i = 0; i < size; i++ ){
217 File exitFile = files[i];
218 if(exitFile.exists()){
219 fileNames[i] = exitFile.getName();
220 if(SDUtil.isExistFiles(exitFile.getAbsolutePath()))
221 filePaths[i] = exitFile.getAbsolutePath();
222 else
223 filePaths[i] = null;
224 }
225 }
226 return size;
227 }
228 }
229 return 0;
230 }
231 }
main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6 <RelativeLayout
7 android:layout_width="fill_parent"
8 android:layout_height="wrap_content"
9 android:background="@color/search_bg_color"
10 >
11 <EditText
12 android:id="@+id/entry"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:background="@android:drawable/editbox_background"
16 android:width="285dp"
17 android:height="35dp"
18 android:layout_marginTop="3dp"
19 />
20 <ImageButton
21 android:id="@+id/btn_imageButton"
22 android:layout_width="fill_parent"
23 android:layout_height="wrap_content"
24 android:text="@string/search_btn"
25 android:background="@drawable/search_image2"
26 android:layout_alignTop="@id/entry"
27 android:layout_toRightOf="@id/entry"
28 android:layout_marginTop="3dp"
29 />
30 </RelativeLayout>
31 <ListView
32 android:id="@+id/listleft"
33 android:layout_width="fill_parent"
34 android:layout_height="wrap_content">
35 </ListView>
36 </LinearLayout>
listinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/titleleftlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/infoleftlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Interface is shown below:
Reproduced in: https: //my.oschina.net/garyun/blog/602986