package com.yql.listviewdemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver.OnScrollChangedListener;
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;
//OnItemClickListener可以处理视图中单个条目的点击事件
//OnScrollListener监测滚动的变化,可以用于视图中滚动加载数据
public class MainActivity extends Activity implements OnItemClickListener,OnScrollListener{
private ListView listView;
private ArrayAdapter<String> array_adapter;
private SimpleAdapter simpleAdapter;
private List<Map<String, Object>> sim_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView1);
//新建一个适配器
//ArrayAdapter(上下文,当前lisview加载的每一个列表项对应的布局,数据源)
//array_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
//listView.setAdapter(array_adapter);
/**
* context:上下文
* data:List<? extends Map<String, ?>> data 一个map组成的list集合
* 每一个Map对应listView中的一行
* 每一个Map(键——值对)键必须包含在from中对应的键
* resource:列表项的布局文件id
* from:map中的键名
* to:绑定的视图中的id,与from中的键对应
*/
sim_list = new ArrayList<Map<String,Object>>();
simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text});
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(this);
listView.setOnScrollListener(this);
}
private List<Map<String, Object>> getData(){
for(int i=0;i<10;i++){
Map<String,Object> map = new HashMap<String, Object>();
map.put("pic",R.drawable.ic_launcher);
map.put("text", "名字"+i);
sim_list.add(map);
}
return sim_list;
}
//获得点击列表项的信息
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
String text = listView.getItemAtPosition(arg2)+"";
Toast.makeText(this, "position:"+arg2+";text:"+text, Toast.LENGTH_SHORT).show();
}
@Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
//滑动事件
@Override
public void onScrollStateChanged(AbsListView view, int state) {
switch(state){
case SCROLL_STATE_FLING:
Log.i("main", "用户离开手指前用力的滑动一下");
//滑动更新数据
Map<String,Object> map = new HashMap<String, Object>();
map.put("pic",R.drawable.ic_launcher);
map.put("text", "手机号");
sim_list.add(map);
simpleAdapter.notifyDataSetChanged();//通知adapter数据改变
break;
case SCROLL_STATE_IDLE:
Log.i("main", "停止滑动");
break;
case SCROLL_STATE_TOUCH_SCROLL:
Log.i("main", "用户手指触摸滑动");
break;
}
}
}
布局文件:
item.xml
<pre name="code" class="html"><?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="match_parent"
android:orientation="horizontal" >
<ImageView
android:layout_marginLeft="15dp"
android:id="@+id/pic"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20sp"
android:text="demo"
/>
</LinearLayout>
main.xml
<pre name="code" class="html"><?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="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>