深入Android- AdapterView與ListView



一,前言


    ListView和GridView作爲一個常用的控件,基本上每一個app都要用到,本文透過相關資料,試圖爲大家解開Android開發中的那些疑問,同時也探討Android部分源代碼的本質。

  本文引用的資料,由於時間關係,不多做翻譯。

  閱讀本文前,我認爲讀者應該具備基本的Android相關知識,如Activity/View的基本原理及使用。


二,相關知識儲備


系統框架


      
      從Android的系統框架結構上看,我們這次要了解的是處於Application Framework層的View部分.


類層次關係


從類的層次關係上可以明顯看到,ViewGroup是View的子類,而ListView和GridView則分別繼承自AdapterView.

ViewGroup:

http://developer.android.com/reference/android/view/ViewGroup.html

ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This class also defines the ViewGroup.LayoutParams class which serves as the base class for layouts parameters.

Also see ViewGroup.LayoutParams for layout attributes.

顧名思義,ViewGroup是一個或者多個View的組合,它提供Layout服務,把多個View組合成一個複雜視圖。

ViewGroup裏面有一個重要的概念:Layout,定義其子視圖的Layout。

      

Layout:

  http://developer.android.com/guide/topics/ui/declaring-layout.html

                   

   A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. You can declare a    layout in two ways:

   Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and    subclasses, such as those for widgets and layouts.
  Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and manipulate their      properties) programmatically.

Layout是一個定義UI控件的可視結構體,可以用XML聲明或運行時實例化,下圖展示了Layout和View之間的關係。

 以下以最常用的LinearLayout作例子,描述Layout的本質,其它幾種Layout本質也是一樣,需要了解的朋友參閱相關資料即可:

 http://developer.android.com/guide/topics/ui/layout/linear.html

 LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.

請注意其描述中的:single direction,這個就是LinearLayout所遵循的規則和限制。

再對比Linear Layout和Relative Layout的差異,本質上是一樣的,只是應用的場景不同。

根據上面的資料,我們不難得出ViewGroup和View之間的關係:

http://www.cnblogs.com/duguguiyu/archive/2010/03/27/1698515.html

        通過上面的一系列的分析,我們基本知道了有關於Android中一個View的來龍去脈,接下來我們會對AdapterView和ListView進行一步的講解。
        繼承層次:
        
        ListView繼承自AdapterView的子類AbsListView,而且ListView跟GridView是兄弟關係。

        AdapterView:

          

        AdapterView是一個以具體的Adapter驅動的視圖,如何去理解這句話:

1,不管ListView,還是GridView,還是Gallery,它的本質是存儲着一分表格形式的數據。而這份數據如何進行展現,根據指定的Adapter,它會呈現成列表,表格或圖庫的模式。

2,AdapterView有具體處理用戶輸入的能力,所以相應的它可以設置listener。

        
         ListView:

            

         
        從上面的分析中,繼續往下解剖之後,剩下的事情很簡單,就是ListView是一個使用了某一種Adapter的線性列表視圖,只要指定具體的Adapter(如ArrayAdapter),就呈現出具體的樣式。
        看到這裏,我相信大家會更明白,爲什麼ListView需要綁定一個Adapter;以及往後如果需要對類似的View進行擴展的時候,應該如何做。

        引入childView:

        由於每一個Layout,本身就是View的子類,所以,當我們需要指定自已的View,可以很簡單的在xml裏面指定一個layout,然後find它,把它轉換成layout,然後把自定義的view加到layout裏面,範例代碼如下:


        public View getView(int position, View convertView, ViewGroup parent) {
              LayoutInflater inflater = (LayoutInflater) context
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              View rowView = inflater.inflate(R.layout.list_analystics, parent, false);
              
              DemoStatistics statistics = new DemoStatistics();
              View subView = statistics.setData(context);
              LinearLayout ly = (LinearLayout) rowView.findViewById(R.id.statistics);
              ly.addView(subView);
              
              return rowView;
        }

        
相關的源代碼在線查看資源有:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.6_r2/android/widget/AdapterView.java?av=f

本文參考了大量的網上資料,因爲比較多,不一一列出。



發佈了17 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章