国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android ListView之EfficientAdapte的使用詳解

2019-10-22 18:31:11
字體:
來源:轉載
供稿:網友

Android ListView之EfficientAdapte的使用詳解

在做Android手機應用開發時, ListView是一個非常常用的控件。如何更新的使用它呢?其實SDK中的例子已經非常的完整了,并且能滿足大多數的需要。

    如果大家剛開始學習ListView,我建議大家還是直接先看官方的例子好了,這樣大家會學到更好的寫法以及養成更好的習慣。

    下面就以EfficientAdapter為例,看看官網例子是如何使用ListView的:

    請大家格外注意getView的書寫方法,大家可能從網上也能找到過一些其它的例子,但是網上的寫法和官網不同,建議大家采用官網例子的寫法。

    簡要說明:要實現高效的Adapter,需要做兩件事: 

    1. 重用getView()中的convertView,避免在不必要的時候inflating View。 

    2. 使用ViewHolder模式,避免在不必要的時候調用findViewById()。

    順便再提一句:若繼承的是ListActivity,如果在layout xml里定義了ListView,那么該ListView的ID必須是"@id/android:list",最好再包含一個ID是"@id/android:empty"的TextView,供ListView中沒有數據時,顯示提示文字用。如下所示:

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> 

    官網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"}; } 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持,如有疑問請留言或者到本站社區交流討論,大家共同進步!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 两当县| 公主岭市| 布尔津县| 民丰县| 鱼台县| 乌兰浩特市| 东丽区| 临洮县| 扬中市| 九龙县| 虹口区| 年辖:市辖区| 辰溪县| 永寿县| 班戈县| 柳州市| 凤凰县| 柳河县| 朔州市| 兴文县| 灵寿县| 瑞丽市| 景宁| 宁化县| 丹巴县| 河津市| 新密市| 道孚县| 湖南省| 镇平县| 岳阳市| 南昌县| 高碑店市| 长春市| 平昌县| 左权县| 南开区| 衡阳县| 广汉市| 积石山| 合作市|