你的位置:首页 > 软件开发 > 操作系统 > 读BaseAdapter的一点感悟

读BaseAdapter的一点感悟

发布时间:2015-03-24 16:02:03
适配器,作为android应用层的开发中,具有很重要的作用。在诸如ListView,gallery等sdk中提供的展示批量数据的控件中,起到一个适配数据源的作用。sdk中已经为我们提供了一个简单的并且适用性很广的适配器SimpleAdapter,该类就是继承自抽象类BaseAda ...

  适配器,作为android应用层的开发中,具有很重要的作用。在诸如ListView,gallery等sdk中提供的展示批量数据的控件中,起到一个适配数据源的作用。sdk中已经为我们提供了一个简单的并且适用性很广的适配器SimpleAdapter,该类就是继承自抽象类BaseAdapter实现的一个具体的适配器,并且能接收 List<? extends Map<String, ?>>形式的数据源格式的数据。但是很多情况下,我们使用ListView等容器的时候,往往要在渲染每一条数据的时候做一些我们自己想做的事情,此时就要实现BaseAdapter来实现自己的一个Adapter。


 

  在进入正文之前,先说一下关于抽象类与接口的区别,以下摘自百度知道。

  你选择使用接口和抽象类的依据是什么? 接口和抽象类的概念不一样。接口是对动作的抽象,抽象类是对根源的抽象。抽象类表示的是,这个对象是什么。接口表示的是,这个对象能做什么。比如,男人,女人,这两个类(如果是类的话……),他们的抽象类是人。说明,他们都是人。人可以吃东西,狗也可以吃东西,你可以把“吃东西”定义成一个接口,然后让这些类去实现它.所以,在高级语言上,一个类只能继承一个类(抽象类)(正如人不可能同时是生物和非生物),但是可以实现多个接口(吃饭接口、走路接口)。第一点. 接口是抽象类的变体,接口中所有的方法都是抽象的。而抽象类是声明方法的存在而不去实现它的类。 第二点. 接口可以继承,抽象类不行 第三点. 接口定义方法,不能实现,而抽象类可以实现部分方法。 第四点. 接口中基本数据类型为static 而抽类象不是的。当你关注一个事物的本质的时候,用抽象类;当你关注一个操作的时候,用接口。 接口可以实现也可以继承,抽象类不行 抽象类的功能要远超过接口,但是,定义抽象类的代价高。因为高级语言来说(从实际设计上来说也是)每个类只能继承一个类。在这个类中,你必须继承或编写出其所有子类的所有共性。虽然接口在功能上会弱化许多,但是它只是针对一个动作的描述。而且你可以在一个类中同时实现多个接口。在设计阶段会降低难度的。

  


 

  上面说的很清楚了,接口是对动作的抽象,而抽象类是对根源的抽象。抽象类表示的是这个对象是什么,而接口表示的是这个对象能做什么。同时在我阅读BaseAdapter源码的时候也有些感悟,BaseAdapter是一个抽象类,它实现了ListAdapter和SpinnerAdapter接口,而这两个接口又实现了Adapter接口。我们在构造自己的Adapter的时候经常要必须实现的getCount()和getView()方法都是在Adapter接口中定义的。

  在大牛们写的源码中,往往都是接口与抽象类同时使用。有关于BaseAdapter中的源码,则把最基本的一些功能(能做什么)抽象为一个接口,放在最底层,而后通过继承这个接口再抽象一些子接口,当有抽象类实现了这些子接口的时候,可以选择性的(这是抽象类允许的)实现接口中定义的功能,作为一个具有统一功能的更具体的但是同样以抽象的形式而存在,最后我们要构造这种经过抽象的类型的具体类的时候,则需要将抽象类没有实现的功能全部实现(这是必须的),甚至可以重写抽象类中已经实现的某些方法(一般情况下不必这样做),这时我们可以通过实例化我们自己构造的具体类来完成一些工作了。

  这种层级关系让我体验到了从无到有的过程,也深刻的理解了什么叫抽象到具体的过程,在这种模型的基础上,我们可以从容的扩展和具象化。不必担心功能的丢失或者偏离设计此类型的初衷,由简化繁需要强大的根基,抽象类和接口的结合就是面向对象变成最强大的根基。

 

 

  下面贴出源码来具体看

  既然上面说到了由简化繁,那么就从最基本的接口开始

  1、Adapter

 1 package android.widget; 2  3 import android.database.DataSetObserver; 4 import android.view.View; 5 import android.view.ViewGroup; 6  7 /** 8  * 。。。。。。 9 */ 10 public interface Adapter { 11   /** 12    * Register an observer that is called when changes happen to the data used by this adapter. 13    * 14    * @param observer the object that gets notified when the data set changes. 15   */ 16   void registerDataSetObserver(DataSetObserver observer); 17  18   /** 19    * Unregister an observer that has previously been registered with this 20    * adapter via {@link #registerDataSetObserver}. 21    * 22    * @param observer the object to unregister. 23   */ 24   void unregisterDataSetObserver(DataSetObserver observer); 25  26   /** 27    * How many items are in the data set represented by this Adapter. 28    *  29    * @return Count of items. 30   */ 31   int getCount();   32    33   /** 34    * Get the data item associated with the specified position in the data set. 35    *  36    * @param position Position of the item whose data we want within the adapter's  37    * data set. 38    * @return The data at the specified position. 39   */ 40   Object getItem(int position); 41    42   /** 43    * Get the row id associated with the specified position in the list. 44    *  45    * @param position The position of the item within the adapter's data set whose row id we want. 46    * @return The id of the item at the specified position. 47   */ 48   long getItemId(int position); 49    50   /** 51    * Indicates whether the item ids are stable across changes to the 52    * underlying data. 53    *  54    * @return True if the same id always refers to the same object. 55   */ 56   boolean hasStableIds(); 57    58   /** 59    * 。。。。。。 60   */ 61   View getView(int position, View convertView, ViewGroup parent); 62  63   /** 64    * An item view type that causes the {@link AdapterView} to ignore the item 65    * view. For example, this can be used if the client does not want a 66    * particular view to be given for conversion in 67    * {@link #getView(int, View, ViewGroup)}. 68    *  69    * @see #getItemViewType(int) 70    * @see #getViewTypeCount() 71   */ 72   static final int IGNORE_ITEM_VIEW_TYPE = AdapterView.ITEM_VIEW_TYPE_IGNORE; 73    74   /** 75    * Get the type of View that will be created by {@link #getView} for the specified item. 76    *  77    * @param position The position of the item within the adapter's data set whose view type we 78    *    want. 79    * @return An integer representing the type of View. Two views should share the same type if one 80    *     can be converted to the other in {@link #getView}. Note: Integers must be in the 81    *     range 0 to {@link #getViewTypeCount} - 1. {@link #IGNORE_ITEM_VIEW_TYPE} can 82    *     also be returned. 83    * @see #IGNORE_ITEM_VIEW_TYPE 84   */ 85   int getItemViewType(int position); 86    87   /** 88    * 。。。。。。 89   */ 90   int getViewTypeCount(); 91    92   static final int NO_SELECTION = Integer.MIN_VALUE; 93  94   /** 95    * @return true if this adapter doesn't contain any data. This is used to determine 96    * whether the empty view should be displayed. A typical implementation will return 97    * getCount() == 0 but since getCount() includes the headers and footers, specialized 98    * adapters might want a different behavior. 99    */100   boolean isEmpty();101 }

原标题:读BaseAdapter的一点感悟

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录