因为经常忘记,总结一下之前学过的各种Adapter。
1. ArrayAdapter
构造函数
public ArrayAdapter (Context context, int resource, T[] objects)
Parameters
context: 上下文.
resource: 资源ID,该资源包含了一个TextView组件(The resource ID for a layout file containing a TextView to use when instantiating views).
objects :在ListView上要呈现的内容(The objects to represent in the ListView).
public class ArrayAdapterTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.array_adapter);
Toast.makeText(this, "button1 pressed!", 0).show();
ListView ls=(ListView) this.findViewById(R.id.listView1);
String arr[]=new String[]{"hello","world","arrayAdapter"};
ArrayAdapter adapter=new ArrayAdapter(this,R.layout.array_adapter_item,arr);
ls.setAdapter(adapter);
}
2. SimpleAdapter
public SimpleAdapter (Context context, List<? extends Map<String, ?>>data, int resource, String[] from, int[] to)
Parameters
context : The context where the View associated with this
SimpleAdapter is running
data : A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in “from”
resource: Resource identifier of a view layout that defines the views
for this list item. The layout file should include at least those
named views defined in “to”
from : A list of column names that will be added to the Map associated with each item.
to : The views that should display column in the “from” parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
for(int i=0;i<names.length;i++){
Map<String,Object> map=new HashMap<String, Object>();
map.put("name", names[i]);
map.put("desc",desc[i]);
map.put("image",imageIds[i]);
listItem.add(map);
}
/*
from参数中的内容为map中的key.set()
*/
SimpleAdapter sa=new SimpleAdapter(this, listItem, R.layout.simple_adapter_item, new String[]{"name","desc","image"}, new int[]{R.id.name,R.id.desc,R.id.imageView1});
ls.setAdapter(sa);
3. 自定义Adapter
简述下其中原理性的东西:
(0) ButterKnifed的框架 : 关于ButterKnife的基础知识可以参考我之前的博客,这里相当于补充其在ViewHolder中的应用
http://blog.csdn.net/csp277/article/details/46787691
(1)setTag的作用 : 就是为了通过对holder的引用,实现缓存后继续试用holder避免了每个页面的每一条item都重新定义holder并获取对应物理组件。
实现原理:
a.java中如果一个对象存在实引用,则JVM不能回收该对象
b.ListView有个循环机制,只有手机显示屏所能够呈现的(容纳的)item(即第一页的items) 是需要新建View的,为了达到高效,当往下划时,Android复用了之前建好的view(convertView),也就是说不用再new
c.结合到一起,当listView向下滑动时,系统复用之前产生的convertView ,从而可以将携带的Tag,即holder对象获取出来,避免了再次新建对象,反复findViewById等操作。
参考:
http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works
public class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public MyAdapter(Context context){
this.mInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return SimpleAdapterTest.names.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder=null;
if(convertView==null){
convertView=mInflater.inflate(R.layout.my_adapter_item, null);
holder= new ViewHolder(convertView);
//setTag的作用就是为了通过对holder的引用,实现缓存后继续试用holder
//避免了每个页面的每一条item都重新定义holder并获取对应物理组件
//原理:1.java中如果一个对象存在实引用,则JVM不能回收该对象
//2.ListView有个循环机制,只有第一个手机显示屏所能够呈现的item
//是需要新建View的,为了达到高效,Android复用了之前建好的view(convertView)
//3.结合到一起,当listView向下滑动时,系统复用之前产生的convertView
//从而可以将携带的Tag,即holder对象获取出来,避免了再次新建对象,反复findViewById等操作
convertView.setTag(holder);
}
else{
holder=(ViewHolder) convertView.getTag();
}
holder.myNameView.setText(SimpleAdapterTest.names[position]);
holder.myDescView.setText(SimpleAdapterTest.desc[position]);
holder.imageView.setImageResource( SimpleAdapterTest.imageIds[position]);
holder.myButton.setText("bn["+(position+1)+"]");
return convertView;
}
public final class ViewHolder{
@InjectView(R.id.my_imageview)
ImageView imageView;
@InjectView(R.id.my_name)
TextView myNameView;
@InjectView(R.id.my_desc)
TextView myDescView;
@InjectView(R.id.my_bn)
Button myButton;
public ViewHolder(View view){
ButterKnife.inject(this, view);
}
}
}
4.运行效果图
这里粗略展示一下的自己写得Demo的粗糙效果
版权声明:本文为博主原创文章,未经博主允许不得转载。