课程目标
1.理解ListView的基础使用
2.学会熟练运用两种适配器(ArrayAdapter、SimpleAdapter)
3.学会熟练运用两种监听器(OnScrollListener、OnItemClickListener)
4.学会数量运用适配器数据的刷新(notifyDataChanged)
ListView
作用:android系统中显示列表的控件
ListView控件(每一个ListView都可以包含很多个列表项)
数据适配器
作用:把复杂的数据(数组、链表、数据库、集合等)填充在制定视图界面上
ArrayAdapter(数组适配器):用于绑定格式单一的数据
数据源:可以是集合或数组
SimpleAdapter(简单适配器):用于绑定格式复杂的数据
数据源:只能是特定泛型的集合
数据适配器是链接数据源和视图见面的桥梁
实现过程:新建适配器->添加数据源到适配器->视图加载适配器
新建一个数组适配器:ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源)
arr_adapter = new ArrayAdapter<Stirng>(this, android.R.layout.simple_list_item_1, arr_data);
视图加载适配器:listView.setAdapter()方法。
ListView with ArrayAdapter:
String[] arrData = new String[] { "apple", "banana", "orange", "pear" };
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrData);
listView.setAdapter(arrayAdapter);
SimpleAdapetr(context, data, resource, from, to)
context:上下文
data:数据源(List<? extends Map<String,?>> data) 一个Map所组成的List集合
每一个Map都回去对应ListView列表中的一行
每一个Map(键-值对)中的键必须包含所有在from中所指定的键
resource:列表项的布局文件ID
from:Map中的键名
to:绑定数据视图中的Id,与from成对应关系
对于resource,我们新建一个样式item.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="30dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_lena" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="TextView"
android:gravity="left|bottom"
/>
</LinearLayout>
item.xml
ListView with SimpleAdapter:
dataList = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 20; i ++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("pic", R.drawable.ic_lena);
map.put("text", "Item number : " + i);
dataList.add(map);
}
simpleAdapter = new SimpleAdapter(this, dataList, R.layout.item, new String[] {"pic", "text"}, new int[] {R.id.pic, R.id.text});
listView.setAdapter(simpleAdapter);
实现点击事件的方法:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = "position=" + position + " item=" + listView.getItemAtPosition(position);
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
实现ListView滑动状态改变时的方法:
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
String scrollStatString = "";
switch (scrollState) {
case SCROLL_STATE_FLING:
scrollStatString = "用户手指离开屏幕之前,由于用力滑了一下,视图仍在继续滑动";
break;
case SCROLL_STATE_IDLE:
scrollStatString = "视图已经停止滑动";
break;
case SCROLL_STATE_TOUCH_SCROLL:
scrollStatString = "手指没有离开屏幕,试图正在滑动";
break;
}
Toast.makeText(this, scrollStatString, Toast.LENGTH_SHORT).show();
}
我们可以通过监听现在的事件是否是滑动来在ListView中新建一个Item:
Map<String, Object> map = new HashMap<String, Object>();
map.put("pic", R.drawable.ic_lena);
map.put("text", "新增项");
dataList.add(map);
但如果直接这么操作会出错,因为虽然通知了程序新增一个Item,但是没有通知UI界面刷新,所以还需要写一行代码使当前的界面刷新:
simpleAdapter.notifyDataSetChanged();
这样就完成了一个简短的类似于下拉刷新的案例。
知识要点及注意事项
(1)SimpleAdapter的构造方法中参数较多,写的时候不要着急,要对应好的布局文件中的id
(2)监听器和适配器是通用于很多android控件上,不局限与ListView
(3)样例通过onScrollListener坚挺了用户手指滑动的动作,实际上我们常见的列表下拉刷新就是依次扩展出来的
(4)熟练运用notifyDatasetchanged。它可以动态更新视图中所包含的数据。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android1:id="@+id/listView1"
android1:layout_width="match_parent"
android1:layout_height="wrap_content" >
</ListView>
</LinearLayout>
activity_main.xml
package com.example.listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements OnItemClickListener, OnScrollListener {
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private SimpleAdapter simpleAdapter;
private List<Map<String, Object>> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
String[] arrData = new String[] { "apple", "banana", "orange", "pear" };
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrData);
listView.setAdapter(arrayAdapter);
dataList = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 20; i ++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("pic", R.drawable.ic_lena);
map.put("text", "Item number : " + i);
dataList.add(map);
}
simpleAdapter = new SimpleAdapter(this, dataList, R.layout.item, new String[] {"pic", "text"}, new int[] {R.id.pic, R.id.text});
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(this);
listView.setOnScrollListener(this);
}
@Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
String scrollStatString = "";
switch (scrollState) {
case SCROLL_STATE_FLING:
scrollStatString = "用户手指离开屏幕之前,由于用力滑了一下,视图仍在继续滑动";
Map<String, Object> map = new HashMap<String, Object>();
map.put("pic", R.drawable.ic_lena);
map.put("text", "新增项");
dataList.add(map);
simpleAdapter.notifyDataSetChanged();
break;
case SCROLL_STATE_IDLE:
scrollStatString = "视图已经停止滑动";
break;
case SCROLL_STATE_TOUCH_SCROLL:
scrollStatString = "手指没有离开屏幕,试图正在滑动";
break;
}
Toast.makeText(this, scrollStatString, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = "position=" + position + " item=" + listView.getItemAtPosition(position);
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
}
MainActivity.java
效果: