這ListView真是麻煩,一個小小的FileBrowser廢了將近2天。看來自己的功力還需要加強。
做FileBrowser中遇到幾個ListView問題總結一下。
1、自定義樣式
ListView其實和Asp.net裏面的Repeater有點像,但是不同的是項的內容可以用一個layout文件來套用,這個比較有意思,畢竟剛開始研究android姑且叫它自定義樣式吧,正好我考慮着後期給FileBrowser寫個換膚的功能,有這個東西就比較easy了。
首先需要這樣的layout文件:
res/layout/imagelist.xml
然後在onCreate中用下面的方法即可將layout裏套用的東西顯示出來
LinearLayout llFile = (LinearLayout) findViewById(R.id.llfiles);
SimpleAdapter listAdapter = new SimpleAdapter(this, GetFiles(),
R.layout.imagelist, new String[] { "img", "file" }, new int[] {
R.id.img, R.id.file });
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(listAdapter);
GetFile方法:
File file = new File(currentPath);
File[] fileList = file.listFiles();
files.clear();
Map<String, Object> mapBack = new HashMap<String, Object>();
mapBack.put("img", R.drawable.folder);
mapBack.put("file", "..");
mapBack.put("checked", false);
Arrays.sort(fileList, ComparorFactory.CreateComparator("FileName"));
files.add(mapBack);
for (File f : fileList) {
Map<String, Object> map = new HashMap<String, Object>();
if (!f.canRead())
continue;
map.put("img", IconFactory.getIcon(f));
map.put("file", f.getName());
files.add(map);
}
return files;
}
現在能看到這樣的界面,但是文件夾不能向上,怎麼辦,
LayoutInflater rl = getLayoutInflater();
View row = rl.inflate(R.layout.imagelist, null, false);
CheckBox cb = (CheckBox) row.findViewById(R.id.cbSelect);
cb.setVisibility(CheckBox.INVISIBLE);
ImageView img = (ImageView) row.findViewById(R.id.img);
img.setImageResource(R.drawable.folder);
TextView txt = (TextView) row.findViewById(R.id.file);
txt.setText("..");
return row;
}
lv.setAdapter(listAdapter); 之前調用lv.addHeaderView(getHeaderView());就可以加上去了。