Android中ListView的总结(1)

摘要:
看来我的技能需要加强。总结FileBrowser中遇到的几个ListView问题。˃˃RelativeLayoutxmlns:android=“http://schemas.android.com/apk/res/android“android:id=”@+id/RelativeLayout01“android:layout_width=”fill_parent“android:layout_height=”wrap _ content“android:ppaddingBottom=”4dip“android:bpaddingLeft=”12dip“android:PpaddingRight=”12dip“˃˂TextViewandroid:text=”TextView01“android:layout_height=”wrap_content“android:textSize=”20dip“android:layout_width=”fill_parent“layout_centerVertical=”true“android:layout_ccenterInParent=”true!

这ListView真是麻烦,一个小小的FileBrowser废了将近2天。看来自己的功力还需要加强。

做FileBrowser中遇到几个ListView问题总结一下。

1、自定义样式

  ListView其实和Asp.net里面的Repeater有点像,但是不同的是项的内容可以用一个layout文件来套用,这个比较有意思,毕竟刚开始研究android姑且叫它自定义样式吧,正好我考虑着后期给FileBrowser写个换肤的功能,有这个东西就比较easy了。

首先需要这样的layout文件:

res/layout/imagelist.xml

<?xml version="1.0" encoding="UTF-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  android:id="@+id/RelativeLayout01" android:layout_width="fill_parent"  
  android:layout_height="wrap_content" android:paddingBottom="4dip"  
  android:paddingLeft="12dip" android:paddingRight="12dip">
     <ImageView android:paddingTop="12dip" android:layout_width="50dip"  
    android:layout_height="50dip" android:id="@+id/img" android:layout_alignParentLeft="true" />  
   <TextView android:text="TextView01" android:layout_height="wrap_content"
    android:textSize="20dip" android:layout_width="fill_parent"
    layout_centerVertical="true" android:layout_centerInParent="true"
    android:paddingLeft="50dip" android:gravity="clip_vertical"
  android:id="@+id/file" />
<CheckBox android:id="@+id/cbSelect" android:layout_width="wrap_content"
     android:focusable="false" <!-- 这个没有的话会让ListView的内容无没点击,
因为CheckBox获得焦点的优先级比ListView要高,不明白为什么要这样设计,
事实是并没有点到CheckBox它也会把下面的ListView的项完全挡住-->
  android:layout_height="wrap_content" android:layout_alignParentRight="true"
  android:layout_centerVertical="true"></CheckBox>
</RelativeLayout>

然后在onCreate中用下面的方法即可将layout里套用的东西显示出来

ListView lv = (ListView) findViewById(R.id.lvFile);
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方法:

public List<Map<String, Object>> GetFiles() {
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;
}
Android中ListView的总结(1)第1张

现在能看到这样的界面,但是文件夹不能向上,怎么办,

private View getHeaderView() {
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());就可以加上去了。

免责声明:文章转载自《Android中ListView的总结(1)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Windows下jupyter notebook 修改打开的浏览器C++ 宏和模板简介下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android-自己定义PopupWindow

Android-自己定义PopupWindow 2014年5月12日 PopupWindow在应用中应该是随处可见的,非经常常使用到,比方在旧版本号的微信其中就用到下拉的PopupWindow。那是自己定义的。新版微信5.2的ActionBar,有人已经模仿了它,但微信详细是使用了ActionBar还是其它的笔者倒是不太清楚。本篇博客主要介绍怎样自己定...

ubuntu13.04下载android4.0.1源码过程

最初我参考的是老罗的博客http://blog.csdn.net/luoshengyang/article/details/6559955 进行下载安装的,但弄着弄着就发现不太对劲了。这里记录下详细过程: 1,我的前提是已经搭建好了Android开发环境,也即jdk已经安装好了,输入java -version来检查是否成功。搭建android开发环境可以...

(转)AppiumLibrary基本操作

*** Settings ***Library AppiumLibraryLibrary CollectionsLibrary StringLibrary Dialogs*** Test Cases ***打开appComment Open Applicationhttp://localhost:4723/wd/hubalias=tudouapp plat...

Android 5

activity_main <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"...

flutter 生成aar文件,嵌入原生android项目

适用于flutter单独开发,android项目单独开发的场景 1:将flutter项目打包成aar文件,详情见官方文档:https://flutter.cn/docs/development/add-to-app/android/add-flutter-screen 2:在原生android项目文件中,项目根目录app/build.gradle文件中添加...

Android学习使用基本界面组件(下拉框,单选框,复选框,数字转轮,滚动条)

(一)建立单选框按钮 RadioGroup和RadioButton建立单选框按钮 字符串资源文件: <resources> <string name="app_name">婚姻建议程序</string> <string name="sex">性别:</string> <...