栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android ListView之EfficientAdapte的使用详解

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android ListView之EfficientAdapte的使用详解

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代码 

 
  
 
    
 
    
  

    官网EfficientAdapter例子如下:

Java代码 

 
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); 
    } 
 
     
    public int getCount() { 
      return DATA.length; 
    } 
 
     
    public Object getItem(int position) { 
      return position; 
    } 
 
     
    public long getItemId(int position) { 
      return position; 
    } 
 
     
    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"}; 
} 

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158254.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号