Android ListView简单使用
最近的项目中用到了Listview,这里记录一下我的使用过程。
1 我的使用场景
在做项目的时候需要做一个历史记录的页面,这个应用每天会在固定的文件夹中生成一个txt文件,只需要把文件夹里面的内容显示在UI界面上,点击某个选项的时候再将文档中的内容显示再UI上就可以,问题比较简单,参考了几篇博客后顺利搞定。主要用到的是ListView控件,这里记录一下。
2 具体实现
首先看布局文件,这里写了两个布局,一个是整体的菜单布局,一个是菜单中的每个选项的布局,首先看整体菜单布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:background="#EDEDED">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1px"
android:divider="#B8B8B8"
android:id="@+id/history_listview">
</ListView>
</LinearLayout>
</ScrollView>
这里使用了简单的Scrollview
再加上一个ListView
,再来看选项的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/spec_item_seq"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp" />
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#B8B8B8"
android:visibility="visible"/>
<TextView
android:id="@+id/spec_item_name"
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="wrap_content"
android:padding="4dp"/>
</LinearLayout>
这里运用了两个TextView
,第一个是序号,第二个是文档的名字,中间的view
控件充当分界线。
布局写好了,那么我们接下来的思路是:
- 获取文件夹中的所有文件名
- 将选项的布局嵌套到整体菜单布局中
- 设置点击监听器
2.1 获取文件名
写好路径,就可以简单的使用file类获取文件夹中的所有文件,我将所有历史记录存储在公共区域的私有部分,卸载应用后不会在手机中残留内容。
private final String HISTORY_PATH = MyApp.mainActivity.getExternalFilesDir(null).toString()+"/HistoryLog";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history_list);
historyLogDir = new File(HISTORY_PATH);
if(!historyLogDir.exists()){
historyLogDir.mkdir();
}
fileList = historyLogDir.listFiles();
initLayout();
}
2.2 simpleAdapter
由于需要展示的内容比较简单,所以简单用了simpleAdapter,通过:
ListView.setAdapter();
可以为一个listview设置一个适配器,这个适配器简单来说就是给他传进来一个List<HashMap<String, value>>
,然后按照键值取内容,将内容放到相应的布局上,这里放下简单的使用代码:
void initLayout(){
historyListView = findViewById(R.id.history_listview);
int seq = 0;
for(File f : fileList){
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("seq", ++seq);
hashMap.put("name", f.getName());
fileInfoList.add(hashMap);
Log.e(TAG, "seq:"+seq+" name:"+f.getName());
}
simpleAdapter = new SimpleAdapter(this,
fileInfoList,
R.layout.history_overview,
new String[]{"seq", "name"},
new int[]{R.id.spec_item_seq, R.id.spec_item_name});//选项布局中的两个textview
historyListView.setAdapter(simpleAdapter);
simpleAdapter.notifyDataSetChanged();
setListViewHeightBasedOnChildren(historyListView);
historyListView.setOnItemClickListener(new myItemClickListener());
}
这里需要注意,写到这为止,只会显示一行,因为没有实时更新listview的高度,这里放一下计算高度的代码:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) { // pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView); // listItem.measure(0, 0);
listItem.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
2.3 点击监听
这个比较简单,无非就是点击时读取txt文件中的内容,然后显示在屏幕上。
private class myItemClickListener implements ListView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String path = fileList[position].getPath();
Log.e(TAG, "path:"+path);
Intent intent = new Intent();
intent.setClass(FuncHistory.this, FuncHistoryShow.class);
intent.putExtra("path", path);
startActivity(intent);
}
}
到此为止整体功能基本实现,这里只是记录一下使用过程,再复杂一点的可能simpleAdapter就不太够用了,到时候可能需要继承ArrayAdapter,写自己的adapter,到时候再进行补充。