package com.example.listview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity_ArrayAdapter extends Activity {
private ListView lv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main = (ListView) findViewById(R.id.lv_main);
//准备一个集合数据
String[] data = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
//准备ArrayAdapter对象
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_array_adapter,data);
//设置Adapter显示列表
lv_main.setAdapter(adapter);
}
}
package com.example.listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity_ArrayAdapter extends Activity {
private ListView lv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main = (ListView) findViewById(R.id.lv_main);
//准备几何数据
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String, Object>();
map.put("icon", R.drawable.lyric_poster_default_bg1_small);
map.put("name", "image--1");
map.put("content", "content--1");
data.add(map);
map = new HashMap<String, Object>();
map.put("icon", R.drawable.lyric_poster_default_bg2_small);
map.put("name", "image--2");
map.put("content", "content--2");
data.add(map);
map = new HashMap<String, Object>();
map.put("icon", R.drawable.lyric_poster_default_bg3_small);
map.put("name", "image--3");
map.put("content", "content--3");
data.add(map);
map = new HashMap<String, Object>();
map.put("icon", R.drawable.lyric_poster_default_bg4_small);
map.put("name", "image--4");
map.put("content", "content--4");
data.add(map);
map = new HashMap<String, Object>();
map.put("icon", R.drawable.lyric_poster_default_bg5_small);
map.put("name", "image--5");
map.put("content", "content--5");
data.add(map);
map = new HashMap<String, Object>();
map.put("icon", R.drawable.lyric_poster_default_bg6_small);
map.put("name", "image--6");
map.put("content", "content--6");
data.add(map);
map = new HashMap<String, Object>();
map.put("icon", R.drawable.profile_default_bg_small);
map.put("name", "image--7");
map.put("content", "content--7");
data.add(map);
map = new HashMap<String, Object>();
map.put("icon", R.drawable.profile_default_comment_bg);
map.put("name", "image--8");
map.put("content", "content--8");
data.add(map);
map = new HashMap<String, Object>();
map.put("icon", R.drawable.profile_default_folder_bg);
map.put("name", "image--9");
map.put("content", "content--9");
data.add(map);
map = new HashMap<String, Object>();
map.put("icon", R.drawable.profile_my_regularly_listen_song_default);
map.put("name", "image--10");
map.put("content", "content--10");
data.add(map);
//准备Map对象中的key的数组,用于得到对应的value
String[] from = {"icon","name","content"};
//Item布局文件中的子View的id的数组
int[] to = {R.id.iv_item_icon,R.id.tv_item_name,R.id.tv_item_content};
//准备SimpleAdapter对象
SimpleAdapter adapter = new SimpleAdapter(
this, data, R.layout.item_simple_adapter, from, to);
//设置Adapter显示列表
lv_main.setAdapter(adapter);
}
}
package com.example.listview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity_BaseAdapter extends Activity {
private ListView lv_main;
private List<ShopInfo> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main = (ListView) findViewById(R.id.lv_main);
//准备几何数据
data = new ArrayList<ShopInfo>();
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg1_small, "image=1", "content=1"));
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg2_small, "image=2", "content=2"));
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg3_small, "image=3", "content=3"));
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg4_small, "image=4", "content=4"));
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg5_small, "image=5", "content=5"));
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg6_small, "image=6", "content=6"));
data.add(new ShopInfo(R.drawable.profile_default_bg_small, "image=7", "content=7"));
data.add(new ShopInfo(R.drawable.profile_default_comment_bg, "image=8", "content=8"));
data.add(new ShopInfo(R.drawable.profile_default_folder_bg, "image=9", "content=9"));
data.add(new ShopInfo(R.drawable.profile_my_regularly_listen_song_default, "image=10", "content=10"));
//准备BaseAdapter对象
MyAdapter adapter = new MyAdapter();
//设置Adapter显示列表
lv_main.setAdapter(adapter);
}
class MyAdapter extends BaseAdapter{
//返回集合数据的总数
@Override
public int getCount() {
return data.size();
}
//返回指定下标对应的数据对象
@Override
public Object getItem(int position) {
return data.get(position);
}
//返回每个条目的ID
@Override
public long getItemId(int position) {
return position;
}
/**
* 返回指定下标的Item的View对象
* position 下标
* convertView 有可用的缓存Item视图对象,前n+1个为null
* parent ListView对象
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//如果没有复用
if (convertView == null) {
//加载Item的布局,得到View对象
convertView = View.inflate(
MainActivity_BaseAdapter.this, R.layout.item_simple_adapter, null);
}
/*
* 根据position设置对应的数据
* 得到当前行的数据对象
*/
ShopInfo shopInfo = data.get(position);
//得到子View对象
ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_item_icon);
TextView nameTV = (TextView) convertView.findViewById(R.id.tv_item_name);
TextView contentTV = (TextView) convertView.findViewById(R.id.tv_item_content);
//设置数据
imageView.setImageResource(shopInfo.getIcon());
nameTV.setText(shopInfo.getName());
contentTV.setText(shopInfo.getContent());
return convertView;
}
}
}
package com.example.listview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView lv_main;
private List<ShopInfo> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main = (ListView) findViewById(R.id.lv_main);
//准备几何数据
data = new ArrayList<ShopInfo>();
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg1_small, "image=1", "content=1"));
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg2_small, "image=2", "content=2"));
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg3_small, "image=3", "content=3"));
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg4_small, "image=4", "content=4"));
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg5_small, "image=5", "content=5"));
data.add(new ShopInfo(R.drawable.lyric_poster_default_bg6_small, "image=6", "content=6"));
data.add(new ShopInfo(R.drawable.profile_default_bg_small, "image=7", "content=7"));
data.add(new ShopInfo(R.drawable.profile_default_comment_bg, "image=8", "content=8"));
data.add(new ShopInfo(R.drawable.profile_default_folder_bg, "image=9", "content=9"));
data.add(new ShopInfo(R.drawable.profile_my_regularly_listen_song_default, "image=10", "content=10"));
//准备BaseAdapter对象
MyAdapter adapter = new MyAdapter();
//设置Adapter显示列表
lv_main.setAdapter(adapter);
}
class MyAdapter extends BaseAdapter{
//返回集合数据的总数
@Override
public int getCount() {
return data.size();
}
//返回指定下标对应的数据对象
@Override
public Object getItem(int position) {
return data.get(position);
}
//返回每个条目的ID
@Override
public long getItemId(int position) {
return position;
}
/**
* 返回指定下标的Item的View对象
* position 下标
* convertView 有可用的缓存Item视图对象,前n+1个为null
* parent ListView对象
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//1.创建或得到对应当前行的一个ViewHolder对象
ViewHolder holder = null;
//如果没有复用
if (convertView == null) {
//加载Item的布局,得到View对象
convertView = View.inflate(
MainActivity.this, R.layout.item_simple_adapter, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.iv_item_icon);
holder.nameTV = (TextView) convertView.findViewById(R.id.tv_item_name);
holder.contentTV = (TextView) convertView.findViewById(R.id.tv_item_content);
//将holder对象保存到convertView上
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
/*
* 根据position设置对应的数据
* 2.得到当前行的数据对象
*/
ShopInfo shopInfo = data.get(position);
//3.给ViewHolder对象的视图设置数据
holder.imageView.setImageResource(shopInfo.getIcon());
holder.nameTV.setText(shopInfo.getName());
holder.contentTV.setText(shopInfo.getContent());
return convertView;
}
//视图的容器类
class ViewHolder{
public ImageView imageView;
public TextView contentTV;
public TextView nameTV;
}
}
}
第0层:不优化
每次执行getView(),都会执行: converterView-View.inflate(R.layouty.XXX);效率太低,在快速滑动是会有卡顿,在数据很多事甚至会内存溢出第1层:复用converterViewif (convertView == null) {convertView = View.inflate(R.layout.XXX);//n+1}每次执行getView()都需要执行converterView.findViewById()得到子View第2层:使用ViewHolder,减少findViewById()的次数Viewholder holder = null;if(converterView == null){converterView-View.inflate(R.layouty.XXX);holder = new ViewHolder();holder.imageView = (ImageView)converterView.findViewById(XXX);holder.textView = (TextView)converterView.findViewById(YYY);converterView.setTag(holder);}else{holder = (Viewholder)converterView.getTag();}Person p = data.get(position);holder.imageView.setImageResource(p.getIcon());holder.textView.setText(p.getName());static classViewHolder{ImageView imageView;TextView textView;}1:对于联网获取列表数据,如果数据太大(比如超多100000条,甚至更多),一次获取出来显示,太慢太耗流量:第4层优化
2:对于联网获取列表数据,如果包含图片数据,每次都请求获取显示,太慢太耗流量:第3层优化
第3层优化:图片三级缓存处理(LRU)
内存缓存 优化加载 速度最快
本地缓存 次优化加载 速度稍快
网络缓存 最后加载 速度有网速决定(浪费流量)
第4层优化:对数据列表进行分页加载显示
通过Scroll监听listView.setonScrollListener(scrollListener),当到达底部时加载下一页列表数据并显示
使用第三方开源框架:Android-PullToRefresh或其他