当前位置: 移动技术网 > IT编程>移动开发>Android > Android ListView之EfficientAdapte的使用详解

Android ListView之EfficientAdapte的使用详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

九把刀之黑暗烈1.7,在海一方社区,铅球加油稿

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持,如有疑问请留言或者到本站社区交流讨论,大家共同进步!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网