Android ListView之EfficientAdapte的使用詳解
Android ListView之EfficientAdapte的使用詳解
在做Android手機(jī)應(yīng)用開(kāi)發(fā)時(shí), ListView是一個(gè)非常常用的控件。如何更新的使用它呢?其實(shí)SDK中的例子已經(jīng)非常的完整了,并且能滿足大多數(shù)的需要。
如果大家剛開(kāi)始學(xué)習(xí)ListView,我建議大家還是直接先看官方的例子好了,這樣大家會(huì)學(xué)到更好的寫(xiě)法以及養(yǎng)成更好的習(xí)慣。
下面就以EfficientAdapter為例,看看官網(wǎng)例子是如何使用ListView的:
請(qǐng)大家格外注意getView的書(shū)寫(xiě)方法,大家可能從網(wǎng)上也能找到過(guò)一些其它的例子,但是網(wǎng)上的寫(xiě)法和官網(wǎng)不同,建議大家采用官網(wǎng)例子的寫(xiě)法。
簡(jiǎn)要說(shuō)明:要實(shí)現(xiàn)高效的Adapter,需要做兩件事:
1. 重用getView()中的convertView,避免在不必要的時(shí)候inflating View。
2. 使用ViewHolder模式,避免在不必要的時(shí)候調(diào)用findViewById()。
順便再提一句:若繼承的是ListActivity,如果在layout xml里定義了ListView,那么該ListView的ID必須是"@id/android:list",最好再包含一個(gè)ID是"@id/android:empty"的TextView,供ListView中沒(méi)有數(shù)據(jù)時(shí),顯示提示文字用。如下所示:
Xml代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
官網(wǎng)EfficientAdapter例子如下:
Java代碼
/**
* Demonstrates how to write an efficient list adapter. The adapter used in this example binds
* to an ImageView and to a TextView for each row in the list.
*
* To work efficiently the adapter implemented here uses two techniques:
* - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
* - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
*
* The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
* getView(). This data structures contains references to the views we want to bind data to, thus
* avoiding calls to findViewById() every time getView() is invoked.
*/
public class List14 extends ListActivity {
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
// Icons bound to the rows.
mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);
mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);
}
/**
* The number of items in the list is determined by the number of speeches
* in our array.
*
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
return DATA.length;
}
/**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position) {
return position;
}
/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new EfficientAdapter(this));
}
private static final String[] DATA = {
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam"};
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,大家共同進(jìn)步!
- Android SQLite事務(wù)處理結(jié)合Listview列表顯示功能示例
- Android 實(shí)現(xiàn)ListView的點(diǎn)擊變色的實(shí)例
- Android Adapter里面嵌套ListView實(shí)例詳解
- Android控件ListView使用方法詳解
- Android使用ListView實(shí)現(xiàn)滾輪的動(dòng)畫(huà)效果實(shí)例
- Android實(shí)現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來(lái)的方法
- ListView用法中與滾動(dòng)相關(guān)的需求實(shí)現(xiàn)
相關(guān)文章
Android編程ProgressBar自定義樣式之動(dòng)畫(huà)模式實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程ProgressBar自定義樣式之動(dòng)畫(huà)模式實(shí)現(xiàn)方法,涉及Android動(dòng)畫(huà)模式的布局技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-10-10
android在root模式下接聽(tīng)來(lái)電的方法
這篇文章主要介紹了android在root模式下接聽(tīng)來(lái)電的方法,需要的朋友可以參考下2014-03-03
解決Android studio模擬器啟動(dòng)失敗的問(wèn)題
這篇文章主要介紹了Android studio模擬器啟動(dòng)失敗的問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
android TextView設(shè)置中文字體加粗實(shí)現(xiàn)方法
android TextView設(shè)置中文字體加粗如何實(shí)現(xiàn),接下來(lái)介紹實(shí)現(xiàn)方法,有需要的朋友可以參考下2013-01-01
Android彈出DatePickerDialog并獲取值的方法
這篇文章主要為大家詳細(xì)介紹了Android彈出DatePickerDialog并獲取值的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
簡(jiǎn)單實(shí)現(xiàn)android短信發(fā)送器
這篇文章主要為大家詳細(xì)介紹了如何簡(jiǎn)單實(shí)現(xiàn)android短信發(fā)送器 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android音視頻開(kāi)發(fā)只硬件解碼組件MediaCodec講解
在Android開(kāi)發(fā)中提供了實(shí)現(xiàn)音視頻編解碼工具M(jìn)ediaCodec,針對(duì)對(duì)應(yīng)音視頻解碼類(lèi)型通過(guò)該類(lèi)創(chuàng)建對(duì)應(yīng)解碼器就能實(shí)現(xiàn)對(duì)數(shù)據(jù)進(jìn)行解碼操作。本文通過(guò)示例詳細(xì)講解了MediaCodec的使用,需要的可以參考一下2023-01-01
Android程序開(kāi)發(fā)之使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載
這篇文章主要介紹了Android程序開(kāi)發(fā)之使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對(duì)象的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對(duì)象的方法,結(jié)合實(shí)例形式分析了Android全局Context的獲取及Intent傳遞對(duì)象的具體操作方法,需要的朋友可以參考下2017-08-08
Android開(kāi)發(fā)之ViewPager實(shí)現(xiàn)滑動(dòng)切換頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)之ViewPager實(shí)現(xiàn)滑動(dòng)切換頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09

