package com.example.test08_listvieew;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class CopyOfMainActivity_Simple extends Activity {
private ListView lv_main;
private List<ShoppInfo> 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<ShoppInfo>();
data.add(new ShoppInfo(R.drawable.f1,"美食名称--1","美食的内容---1"));
data.add(new ShoppInfo(R.drawable.f2,"美食名称--2","美食的内容---2"));
data.add(new ShoppInfo(R.drawable.f3,"美食名称--3","美食的内容---3"));
data.add(new ShoppInfo(R.drawable.f4,"美食名称--4","美食的内容---4"));
data.add(new ShoppInfo(R.drawable.f5,"美食名称--5","美食的内容---5"));
data.add(new ShoppInfo(R.drawable.f6,"美食名称--6","美食的内容---6"));
data.add(new ShoppInfo(R.drawable.f7,"美食名称--7","美食的内容---7"));
data.add(new ShoppInfo(R.drawable.f8,"美食名称--8","美食的内容---8"));
data.add(new ShoppInfo(R.drawable.f9,"美食名称--9","美食的内容---9"));
data.add(new ShoppInfo(R.drawable.f10,"美食名称--10","美食的内容---10"));
//准备适配器对象 BaseAdatper
MyAdapter adapter = new MyAdapter();
//
lv_main.setAdapter(adapter);
}
package com.example.test08_listvieew;
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.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity_Array 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>>();
HashMap<String, Object> mp = new HashMap<String, Object>();
mp.put("icon", R.drawable.f1);
mp.put("name", "美食---1");
mp.put("content", "内容--1");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f2);
mp.put("name", "美食---2");
mp.put("content", "内容--2");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f3);
mp.put("name", "美食---3");
mp.put("content", "内容--3");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon",R.drawable.f4);
mp.put("name", "美食---4");
mp.put("content", "内容--4");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f5);
mp.put("name", "美食---5");
mp.put("content", "内容--5");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f6);
mp.put("name", "美食---6");
mp.put("content", "内容--6");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f7);
mp.put("name", "美食---7");
mp.put("content", "内容--7");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f8);
mp.put("name", "美食---8");
mp.put("content", "内容--8");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f9);
mp.put("name", "美食---9");
mp.put("content", "内容--9");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f10);
mp.put("name", "美食---10");
mp.put("content", "内容--10");
data.add(mp);
//map对象的key数组 ,用于得到对应的value
String[] from={"icon","name","content"};
//item布局文件中的子View的id的数组
int[] to={R.id.iv_item_icon,R.id.iv_item_name,R.id.iv_item_content};
//准备SimpleAdapter对象
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_simple_adapter, from, to);
//设置Adapter显示列表
lv_main.setAdapter(adapter);
}
}
@Override//返回集合的总数
public int getCount() {
return data.size();
}
@Override//返回指定下标对应的数据对象
public Object getItem(int position) {
return data.get(position);
}
@Override//返回每个条目的id 下标
public long getItemId(int position) {
return position;
}
@Override//返回指定下标所对应(Item)的视图对象
//position 下标 parent listView对象
//convertView 可复用Item的视图对象 前n+1个为null
public View getView(int position, View convertView, ViewGroup parent) {
ViewHodler holder=null;
//如果没有复用
if (convertView==null) {
//加载条目(Item)布局 得到视图(View)对象
convertView= View.inflate(CopyOfMainActivity_Simple.this,
R.layout.item_simple_adapter, null);
holder=new ViewHodler();
holder.inv = (ImageView) convertView.findViewById(R.id.iv_item_icon);
holder.nametext = (TextView) convertView.findViewById(R.id.iv_item_name);
holder.inv2 = (TextView) convertView.findViewById(R.id.iv_item_content);
//将holder 保存在convertView上
convertView.setTag(holder);
}else{
//复用时 取出holder
holder=(ViewHodler) convertView.getTag();
}
//2. 得到当前行的对象
ShoppInfo shoppInfo = data.get(position);
//3.给ViewHolder对象的数据设置数据
holder.inv.setImageResource(shoppInfo.getIcon());
holder.nametext.setText(shoppInfo.getName());
holder.inv2.setText(shoppInfo.getContent());
return convertView;
}//视图容器类
class ViewHodler{
public ImageView inv;
public TextView nametext;
public TextView inv2;
}
}
}
package com.example.test08_listvieew;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity 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","J","K","L","O"};
//准备适配器ArrayAdapter对象
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(this, R.layout.item_array,data);
//设置adapter显示列表
lv_main.setAdapter(adapter);
}
}
<?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/iv_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_marginLeft="10dp"
>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
<?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/iv_item_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/f1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="70dp"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
>
<TextView
android:id="@+id/iv_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
<TextView
android:id="@+id/iv_item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
package com.example.test08_listvieew;
public class ShoppInfo {
private int icon;
private String name;
private String content;
public ShoppInfo() {
super();
}
public ShoppInfo(int icon, String name, String content) {
super();
this.icon = icon;
this.name = name;
this.content = content;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "ShoppInfo [icon=" + icon + ", name=" + name + ", content="
+ content + "]";
}
}
第0层:
不优化
每次都会执行getView(),都会执行 convertView=View.inflate(R.layout.XXX);
问题:效率太低,再快速滑动时会有卡顿,在数据多的时候甚至内存溢出
第一层:
复用convertView
if(convertView==null){
convertView=View.inflate(R.layout.XXX); //n+1
}
问题: 每次执行getview()都需要执行convertView.findViewByid()得到子view
第二层:
使用ViewHolder,减少findViewByid()的次数 减到n+1次
ViewHolder holder=null;
if(convertView==null){
convertView=View.inflate(R.layout.XXX); //n+1
holder=new ViewHolder();
holder.imageView=(ImageView)convertView.findViewById(xxx);
holder.textView=(TextView)convertView.findViewById(yyy);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
person p= data.get(position);//之前创建的类
holder.imageView.setImageResource(p.getIco());
holder.textView.setText(p.getName());
static class ViewHolder(){
ImageView imageView;
TextView textView;
}
问题1:
对于联网获取列表数据,如果数据量太大(比如超过100000)条甚至更多一次取出来显示,太慢太消耗流量
问题2.对于联网获取列表数据,如果包含图片数据,每次请求获取显示,太慢太耗流量
第三层:
图片三级缓存处理(LRu算法)
1.内存缓存,优先加载,速度最快
2.本地缓存,次优先加载。速度稍快
3.网络缓存,最后加载,速度由网络速度决定(浪费流量)
第4层:
对数据列表进行分页加载显示
1.自己做:通过Scroll监听
listView。setOnScrollListener(scrollListenter),当到达底部时加载下一页列表数据并显示
2.使用第三方开源框架:Aandroid-PullToRefresh或其他。