Adapter的getView方法详解

如果你有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 abstractViewgetView(int position,ViewconvertView,ViewGroupparent)

Since:API Level 1

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 useinflate(int, android.view.ViewGroup, boolean)to specify a root view and to prevent attachment to the root.

Parameters

positionThe position of the item within the adapter’s data set of the item whose view we want.

convertViewThe 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 (seeand).

parentThe parent that this view will eventually be attached to

ReturnsA 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<String> listdata = new ArrayList<String>(); for(int i=0;i<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<String> listdata; private Context context; public MyAdapter(Context context,ArrayList<String> listdata){ this.context = context; this.listdata = listdata; }@Overridepublic int getCount() {// TODO Auto-generated method stubreturn listdata.size();}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn listdata.get(arg0);}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {// TODO Auto-generated method stubLog.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中大家好好观察相关的输出结果,便可以将其理解

失败是什么?没有什么,只是更走近成功一步;

Adapter的getView方法详解

相关文章:

你感兴趣的文章:

标签云: