Adapter的getView
http://blog.csdn.net/yelbosh/article/details/7831812
BaseAdapter就Android應用程序中經常用到的基礎數據適配器,它的主要用途是將一組數據傳到像ListView、Spinner、Gallery及GridView等UI顯示組件,它是繼承自接口類Adapter,我們經常使用的ListView 的adapter,即SimpleAdapter,是繼承自BaseAdapter的,BaseAdapter是一個基類,沒有實現綁定數據的功能,SimpleAdapter實現了基本控件的綁定,如TextView,Button,ImageView).已經為我們實現好了數據優化工作,這些適配器使用相同組件動態綁定數據的方式進行優化。為什么需要優化呢?因為如果我們有上億個項目要顯示怎么辦?為每個項目創建一個新視圖?這不可能,因為內存有限制。實際上Android為你緩存了視圖。Android中有個叫做Recycler的構件,下圖是他的工作原理:
如果你有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?getView?(int position,?View?convertView,?ViewGroup?parent)
Since:?API Level 1Get 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<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;
? ?? }
@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中大家好好觀察相關的輸出結果,便可以將其理解
轉載于:https://www.cnblogs.com/tianzijiaozi/p/7506692.html
總結
以上是生活随笔為你收集整理的Adapter的getView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个好听的报纸名字。
- 下一篇: 月光石多少钱一克