ListView总结
2024-08-25 16:55:44
ListView类作为在Android开发中经常会使用到的组件,作为新手,还是感到这一块变化形式还是很多的,需要慢慢学习。现在这里大概总结一下。
基于数组的ListView:使用android:entries属性可以指定列表项数组,这种方式最简洁方便,但内容只能是文本,可定制的内容少之又少。
使用ArrayAdapter创建ListView:也仅限于将数组或集合里的元素包装成列表项,比直接使用数组好在,可以指定列表项的列表项组件。例如:使用TextView作为列表项组件,不过也只能使用TextView。
例子:
1.列表项布局文件arraylist_item.xml:是的,没错,布局文件只包含一个TextView,这也是行的。
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"/>
2.主界面布局文件activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.zjlyyq.test.MainActivity"> <ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView" />
</LinearLayout>
3.MainActivity:
package com.example.zjlyyq.test; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends AppCompatActivity {
ListView listView;
String[] names = new String[]{"晃过天空","芝加哥公牛队","但使东山谢安石","颠三倒四","临时监护人"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.arraylist_item,names);
listView.setAdapter(adapter);
}
}
使用SimpleAdapter创建ListView:SimpleAdapter可以满足更强的定制,每个列表项对应一个布局文件,将一个Map里的元素对应在布局文件里的各组件,例如:
1.列表项布局文件simpleadapter_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_weight="0.15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/header"/>
<LinearLayout
android:layout_weight="0.85"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/username"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/talk"/>
</LinearLayout>
</LinearLayout>
2.MainActivity:
package com.example.zjlyyq.test; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MainActivity extends AppCompatActivity {
ListView listView;
String[] names = new String[]{"晃过天空","芝加哥公牛队","但使东山谢安石","颠三倒四","临时监护人"};
int[] headers = new int[]{R.drawable.plane,R.drawable.plane,R.drawable.plane,R.drawable.plane,R.drawable.plane};
String[] talks = new String[]{"晚上吃了吗?","吃了","你呢?","还没?","哦哦"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
List<Map<String,Object>> itemslist = new ArrayList<Map<String,Object>>();
for(int i = 0;i < 5;i ++){
Map<String,Object> evetyItem = new HashMap<String,Object>();
evetyItem.put("header",headers[i]);
evetyItem.put("name",names[i]);
evetyItem.put("talk",talks[i]);
itemslist.add(evetyItem);
}
//创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this,itemslist,R.layout.simpleadapter_item,
new String[]{"header","name","talk"},
new int[]{R.id.header,R.id.username,R.id.talk}); listView.setAdapter(simpleAdapter);
}
}
最新文章
- jq size()与length的区别
- php中文乱码问题
- IE7浏览器下CSS属性选择器二三事
- 如何优雅的写C++代码(一)
- poj 3237 Tree 树链剖分
- 【周期串】NYOJ-1121 周期串
- http://codepen.io/zhou-yg/pen/NqgPmg 在线编辑器
- 前端自动化构建工具-yoman浅谈
- 【Java学习笔记之十一】Java中常用的8大排序算法详解总结
- Sublime Text3 快捷键汇总及设置快捷键配置环境变量
- 06 intent flag三种属性
- 用samba来创建windows下的文件共享
- Spring的诞生
- AngularJS集合数据遍历显示
- Apache 和 Tomcat联系和区别
- springboot读取配置注解@ConfiguratioinProperties和@Value的区别
- CentOS 7 安装Redis
- 分布式监控系统Zabbix-添加windows监控主机
- JVM 内部原理(四)— 基本概念之 JVM 结构
- [Laravel] 10 - WEB API : wrapper