![Adapter適配器深刻詳解_第1頁](http://file4.renrendoc.com/view/edbeb071e90918d41c6166fea228c078/edbeb071e90918d41c6166fea228c0781.gif)
![Adapter適配器深刻詳解_第2頁](http://file4.renrendoc.com/view/edbeb071e90918d41c6166fea228c078/edbeb071e90918d41c6166fea228c0782.gif)
![Adapter適配器深刻詳解_第3頁](http://file4.renrendoc.com/view/edbeb071e90918d41c6166fea228c078/edbeb071e90918d41c6166fea228c0783.gif)
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
AndroidAdapter深刻詳解我在剛玩android時候,對這個adapter很不理解,到底是什么原理呢? 適配器,哎,只知道setAdapter()把參數(shù)傳進去,系統(tǒng)就顯示出來了。今天,針對這個東西,我們做個系統(tǒng)詳細的分析.listview加載adapter過程是這樣的.1先判斷adapter有多少數(shù)據(jù)項,根據(jù)這個數(shù)據(jù)確定有多少item.確定每個item里加載哪個View.把View里加載要顯示的數(shù)據(jù).問提一個一個來解決. 第一個問題:因為adapter都要關(guān)聯(lián)一個list.有來存儲數(shù)據(jù).list的項數(shù)就是Item的數(shù)目. 我們在重載BaseAdapter時候,都要實現(xiàn)這個函數(shù)publicintgetCount(){returnweatherList.size();}哎,這個函數(shù)就是確定關(guān)聯(lián)條目的.第二個問題哪來的view呢, 當(dāng)然我們自己創(chuàng)建的.重載BaseAdapter時候你要實現(xiàn)getView()這個函數(shù),就是這個view.第三個問題,你自己創(chuàng)建的view.加載哪些數(shù)據(jù)你該知道的.呵呵.張豪就喜歡看例子,這個小伙子技術(shù),管理都很牛,得以他為榜樣. 得努力.publicclassCustomAdapterActivityextendsListActivity{/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);ArrayList<Weather>weatherList=newArrayList<Weather>();Weatherw=newWeather("London",17,Weather.OVERCAST);weatherList.add(w);w=newWeather("Paris",22,Weather.OVERCAST);weatherList.add(w);w=newWeather("Athens",29,Weather.SUNNY);weatherList.add(w);w=newWeather("Stockholm",12,Weather.RAIN);weatherList.add(w);WeatherAdapterweatherAdapter=newWeatherAdapter(this,weatherList);setListAdapter(weatherAdapter);}}哎,這個大家都很清楚,關(guān)鍵問題是weatherAdapter哪來的呢? 自己創(chuàng)建的啊,如果創(chuàng)建呢?publicclassWeatherAdapterextendsBaseAdapter{privateContextcontext;privateList<Weather>weatherList; 這就是adapter關(guān)聯(lián)的List,用來存儲數(shù)據(jù).還記的ArrayList要往里傳參數(shù)嗎? 傳的也是這個類型啊.呵呵publicWeatherAdapter(Contextcontext,List<Weather>weatherList){this.context=context;this.weatherList=weatherList;}publicintgetCount(){returnweatherList.size();}publicObjectgetItem(intposition){returnweatherList.get(position);}publiclonggetItemId(intposition){returnposition;}publicViewgetView(intposition,ViewconvertView,ViewGroupparent){Weatherweather=weatherList.get(position);returnnewWeatherAdapterView(this.context,weather);}}哎,這段告訴了我們,有多少個Item,可以通過getCount()得到了。 可是View哪來的呢?當(dāng)然是getView哎,這段告訴了我們,有多少個Item,這個view的獲取就多中多樣了,我們可以傳個LayoutID.通過Inflater出來,也可以自己創(chuàng)建個,只要出來就行.在這里,我們自己創(chuàng)建個View.這個View.是個VIewGroup.classWeatherAdapterViewextendsLinearLayout{publicstaticfinalStringLOG_TAG="WeatherAdapterView";publicWeatherAdapterView(Contextcontext,Weatherweather){super(context);this.setOrientation(HORIZONTAL);LinearLayout.LayoutParamscityParams=newLinearLayout.LayoutParams(100,LayoutParams.WRAP_CONTENT);cityParams.setMargins(1,1,1,1);TextViewcityControl=newTextView(context);cityControl.setText(weather.getCity());addView(cityControl,cityParams);LinearLayout.LayoutParamstemperatureParams=newLinearLayout.LayoutParams(20,LayoutParams.WRAP_CONTENT);temperatureParams.setMargins(1,1,1,1);TextViewtemperatureControl=newTextView(context);temperatureControl.setText(Integer.toString(weather.temperature));addView(temperatureControl,temperatureParams);LinearLayout.LayoutParamsskyParams=newLinearLayout.LayoutParams(25,LayoutParams.WRAP_CONTENT);ImageViewskyControl=newImageView
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 施工方案對工程建設(shè)的經(jīng)濟效益分析
- 跨學(xué)科視角下的情感教育實踐研究
- 音色感知在小學(xué)音樂欣賞中的重要性及其教學(xué)方法
- 藝術(shù)設(shè)計與宗教文化的互動商業(yè)空間的創(chuàng)新之路
- DB3715T 71-2025楊樹退化林修復(fù)技術(shù)規(guī)程
- 二手設(shè)備轉(zhuǎn)讓合同模板
- 2025年雜志宣傳合作協(xié)議(合同)
- 個人房屋買賣合同模板大全
- 二手房銷售合同模板大全
- 個人信用借款擔(dān)保合同范本
- 停車場管理外包服務(wù)合同
- 第八講 發(fā)展全過程人民民主PPT習(xí)概論2023優(yōu)化版教學(xué)課件
- 王崧舟:學(xué)習(xí)任務(wù)群與課堂教學(xué)變革 2022版新課程標準解讀解析資料 57
- 招投標現(xiàn)場項目經(jīng)理答辯(完整版)資料
- 運動競賽學(xué)課件
- 重大事故隱患整改臺賬
- 2022年上海市初中畢業(yè)數(shù)學(xué)課程終結(jié)性評價指南
- 高考作文備考-議論文對比論證 課件14張
- 新華師大版七年級下冊初中數(shù)學(xué) 7.4 實踐與探索課時練(課后作業(yè)設(shè)計)
- 山東省萊陽市望嵐口礦區(qū)頁巖礦
- 《普通生物學(xué)教案》word版
評論
0/150
提交評論