---
title: "Adapter的getView方法详解"
author: "2129286"
date: "2014-05-13T14:09:23Z"
tags: []
categories: ["移动开发"]
slug: "adapter-getview"
draft: false
lastmod: "2014-05-13T14:09:23Z"
id: 2065805
url: "https://2129286.iteye.com/blog/2065805"
---

BaseAdapter就Android应用程序中经常用到的基础数据适配器，它的主要用途是将一组数据传到像ListView、Spinner、Gallery及GridView等UI显示组件，它是继承自接口类Adapter，我们经常使用的ListView 的adapter，即SimpleAdapter，是继承自BaseAdapter的，BaseAdapter是一个基类，没有实现绑定数据的功能，SimpleAdapter实现了基本控件的绑定，如TextView,Button,ImageView).已经为我们实现好了数据优化工作，这些适配器使用相同组件动态绑定数据的方式进行优化。为什么需要优化呢？因为如果我们有上亿个项目要显示怎么办？为每个项目创建一个新视图？这不可能，因为内存有限制。实际上Android为你缓存了视图。Android中有个叫做Recycler的构件，下图是他的工作原理：

![](http://my.csdn.net/uploads/201208/05/1344138579_7567.jpg)

如果你有10亿个项目(item)，其中只有可见的项目存在内存中，其他的在Recycler中。其实我的理解Recyler就是一个队列，用来存储不在屏幕范围内的item，如果item滚出屏幕范围，那么就入队，这里的滚出是完全滚出，即边界等也要完全滚出。如果新的item要滚进来，那么android系统的framework就会查看Recyler是否含有可以重复使用的iew，如果有那么就重新设置该iew 的数据源，然后显示，即出队。那么这么多的item其实只需要占用一定空间的内存，这个内存大小是多少呢？我的感觉是手机屏幕所包含的item的个数，再加上1，然后乘以每个item占用的内存。但是最后我发现是加上2.可能是为了使得缓存更大吧。。。。但是为什么加上2，大家应该理解，如果你不理解，那你就把滚动list的过程好好想一想。那个队列无非就是一个缓存罢了，因为我们的目的是通过那个缓存来重复使用那些已经创建的iew。

使用BaseAdapter的话需要重载四个方法，这些方法分别是getCount，getItem，getItemId，最后一个最重要的是getView，getView函数为什么重要呢？因为它是用来刷新它所在的ListView的。它在什么时候调用的呢？就是在每一次item从屏幕外滑进屏幕内的时候，或者程序刚开始的时候创建第一屏item的时候。看getView的api：

 

#### []()public abstract [View](http://localhost:8080/android/docs/reference/android/view/View.html) getView (int position, [View](http://localhost:8080/android/docs/reference/android/view/View.html) convertView, [ViewGroup](http://localhost:8080/android/docs/reference/android/view/ViewGroup.html) parent)

Since: [API Level 1](http://localhost:8080/android/docs/guide/appendix/api-levels.html#level1)

Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use `inflate(int, android.view.ViewGroup, boolean)` to specify a root view and to prevent attachment to the root.

##### []()Parameters

position The position of the item within the adapter's data set of the item whose view we want. convertView The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see `getViewTypeCount()` and `getItemViewType(int)`). parent The parent that this view will eventually be attached to

##### []()Returns

- A View corresponding to the data at the specified position.

position是指当前dataset的位置，通过getCount和getItem来使用。如果list向下滑动的话那么就是最低端的item的位置，如果是向上滑动的话那就是最上端的item的位置。conert是指可以重用的视图，即刚刚出队的视图。parent应该就是list。

 

为了让大家更好的理解这个函数，写了一个程序，请看：

public class MainActivity extends Activity {

    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity\_main);  
        ListView lv = (ListView)findViewById(R.id.listview);  
        ArrayList&lt;String&gt; listdata = new ArrayList&lt;String&gt;();  
        for(int i=0;i&lt;400;i++){  
        listdata.add("Item" + i);  
        }  
        lv.setAdapter(new MyAdapter(this, listdata));  
    }

    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        getMenuInflater().inflate(R.menu.activity\_main, menu);  
        return true;  
    }  
    public class MyAdapter extends BaseAdapter{  
    private ArrayList&lt;String&gt; listdata;  
    private Context context;  
      
    public MyAdapter(Context context,ArrayList&lt;String&gt; listdata){  
    this.context = context;  
    this.listdata = listdata;  
    }  
@Override  
public int getCount() {  
// TODO Auto-generated method stub  
return listdata.size();  
}

@Override  
public Object getItem(int arg0) {  
// TODO Auto-generated method stub  
return listdata.get(arg0);  
}

@Override  
public long getItemId(int arg0) {  
// TODO Auto-generated method stub  
return arg0;  
}

@Override  
public View getView(int arg0, View arg1, ViewGroup arg2) {  
// TODO Auto-generated method stub  
Log.i("position",arg0+"");  
TextView tmView;  
String temstr;  
if(arg1 == null){  
TextView tm = new TextView(context);  
tmView = tm;  
tmView.setTag("old" + arg0);  
tmView.setText(listdata.get(arg0));  
}else {  
tmView = (TextView)arg1;  
tmView.setText(listdata.get(arg0) + "\\t" + arg1.getTag());  
}

return tmView;  
}  
      
    }

}

从log中大家好好观察相关的输出结果，便可以将其理解