abstract:ListView就是用来显示一行一行的条目的MVC结构 * M:model模型层,要显示的数据 ————people集合 * V:view视图层,用户看到的界面 &n
ListView
就是用来显示一行一行的条目的
MVC结构
* M:model模型层,要显示的数据 ————people集合
* V:view视图层,用户看到的界面 ————ListView
* c:control控制层,操作数据如何显示 ————adapter对象
每一个条目都是一个View对象
BaseAdapter
* 必须实现的两个方法
* 第一个
//系统调用此方法,用来获知模型层有多少条数据
@Override
public int getCount() {
return people.size();
}
* 第二个
//系统调用此方法,获取要显示至ListView的View对象
//position:是return的View对象所对应的数据在集合中的位置
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getView方法调用" + position);
TextView tv = new TextView(MainActivity.this);
//拿到集合中的元素
Person p = people.get(position);
tv.setText(p.toString());
//把TextView的对象返回出去,它会变成ListView的条目
return tv;
}
屏幕上能显示多少个条目,getView方法就会被调用多少次,屏幕向下滑动时,getView会继续被调用,创建更多的View对象显示至屏幕
条目的缓存
当条目划出屏幕时,系统会把该条目缓存至内存,当该条目再次进入屏幕,系统在重新调用getView时会把缓存的条目作为convertView参数传入,但是传入的条目不一定是之前被缓存的该条目,即系统有可能在调用getView方法获取第一个条目时,传入任意一个条目的缓存
代码如下:
activity_main.xml:
item.xml
MainActivity.java
public class MainActivity extends Activity { Listl; ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); //准备集合数据 l = new ArrayList (); l.add(new shopInfo(R.drawable.xing, "name-1", "content-1")); l.add(new shopInfo(R.drawable.xing, "name-2", "content-2")); l.add(new shopInfo(R.drawable.xing, "name-3", "content-3")); l.add(new shopInfo(R.drawable.xing, "name-4", "content-4")); l.add(new shopInfo(R.drawable.xing, "name-5", "content-5")); l.add(new shopInfo(R.drawable.xing, "name-6", "content-6")); l.add(new shopInfo(R.drawable.xing, "name-7", "content-7")); l.add(new shopInfo(R.drawable.xing, "name-8", "content-8")); //准备BaseAdapter对象 MyAdapter a = new MyAdapter(); //设置Adapter显示数据 lv.setAdapter(a); } //这个类可以写在里面,也可以写在外面 public class MyAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return l.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub // 加载item的布局 View view = View.inflate(MainActivity.this, R.layout.item, null); shopInfo s = l.get(position); ImageView i = (ImageView) view.findViewById(R.id.iv); TextView t1 = (TextView) view.findViewById(R.id.t1); TextView t2 = (TextView) view.findViewById(R.id.t2); i.setImageResource(s.getIcon()); t1.setText(s.getName()); t2.setText(s.getContent()); convertView = view ; return convertView; } } }
shopInfo.java
//每行item的数据信息封装类 public class shopInfo { private int icon; private String name; private String 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; } public shopInfo(int icon, String name, String content) { super(); this.icon = icon; this.name = name; this.content = content; } @Override public String toString() { return "shopInfo [icon=" + icon + ", name=" + name + ", content=" + content + "]"; } public shopInfo() { super(); // TODO Auto-generated constructor stub } }