ListView
两种格式的适配器
普通的数组适配器
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements OnItemClickListener , OnScrollListener{
private ListView listView ;
private ArrayAdapter<String>arr_adapter ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView) ;
//1.新建一个数据适配器
/*
* ArrayAddapter(上下文,当前listview加载的列表下所对应的布局文件,数据源)
* 2.适配器加载数据源
* 3.视图加载是适配器*/
String[] arr_data ={"wangmeixia","huanglianghai","wangyanan"} ;
arr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr_data) ;
listView.setAdapter(arr_adapter) ;
}
}
简单适配器
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="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:text="demo"
/>
</LinearLayout>
main.java
package com.example.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.Menu;
import android.view.View;
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 Activity implements OnItemClickListener , OnScrollListener{
private ListView listView ;
private ArrayAdapter<String>arr_adapter ;
private SimpleAdapter simp_adapter ;
private List<Map<String,Object>>datalist ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView) ;
//1.新建一个数据适配器
/*
* ArrayAddapter(上下文,当前listview加载的列表下所对应的布局文件,数据源)
* 2.适配器加载数据源
* 3.视图加载是适配器*/
String[] arr_data ={"wangmeixia","huanglianghai","wangyanan"} ;
arr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr_data) ;
/*
* context:上下文
* data: 数据源(List<? extends Map<String,?>> data) 一个Map所组成的List集合
* 每一个Map都会去对应ListView列中的一行
* 每一个Map (键值对)中的键必须包含所有在from中所指定的键
* resource: 列表中的布局文件ID
* form: Map中的名
* to: 绑定数据源视图中的ID ,与from对应
* */
datalist = new ArrayList<Map<String,Object>>() ;
simp_adapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic","text"},
new int[]{R.id.pic,R.id.text}) ;
//listView.setAdapter(arr_adapter) ;
listView.setAdapter(simp_adapter) ;
listView.setOnItemClickListener(this) ;
listView.setOnScrollListener(this) ;
}
private List<Map<String,Object>> getData(){
for(int i=0 ; i<20 ;i++){
Map<String,Object>map = new HashMap<String, Object>() ;
map.put("pic", R.drawable.ic_launcher) ;
map.put("text", "wangmeixia"+i) ;
datalist.add(map) ;
}
return datalist ;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
switch(scrollState){
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", "增加xiang") ;
datalist.add(map) ;
//刷新后才会显示增加项
simp_adapter.notifyDataSetChanged() ;
break ;
case SCROLL_STATE_IDLE :
Log.i("Main","视图已经停止华东") ;
break ;
case SCROLL_STATE_TOUCH_SCROLL :
Log.i("Main", "手指没有离开屏幕") ;
break ;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
// TODO Auto-generated method stub
String text = listView.getItemAtPosition(position)+"" ;
Toast.makeText(this, "position"+position+"Text="+text, Toast.LENGTH_SHORT).show() ;
}
}