Android 深入理解BaseAdapter和實現ListView並處理點擊事件

這裏主要講解使用BaseAdapter來實現對ListView的Item內容自定義填充,重寫後各個方法的作用,以及Item點擊事件。

查看源碼可以看到BaseAdapter實現了ListAdapter,SpinnerAdapter這兩個接口,繼續查看,發現這兩個接口繼承於接口Adapter。

廢話不多說,上源碼。

方法int getcount()的源碼:

 /**
     * How many items are in the data set represented by this Adapter.
     * 
     * @return Count of items.
     */
    int getCount();   

意思大概是:此適配器所代表的數據集中有多少項目,返回適配器的item數目。

方法long getItem(int position)的源碼:

/**
     * Get the data item associated with the specified position in the data set.
     * 
     * @param position Position of the item whose data we want within the adapter's 
     * data set.
     * @return The data at the specified position.
     */
    Object getItem(int position);

意思大概是:獲取與數據集中指定位置關聯的數據項。

                    position參數解釋:我們想要適配器中的數據集的項目位置,即position。返回指定位置的數據,這裏的指定位置是指點擊的位置。

方法long getItemId(int position)的源碼:

/**
     * Get the row id associated with the specified position in the list.
     * 
     * @param position The position of the item within the adapter's data set whose row id we want.
     * @return The id of the item at the specified position.
     */
    long getItemId(int position);

意思大概是:獲取與列表中指定位置關聯的行ID。

                     position參數解釋:我們想要指定item位置的適配器的數據集的行id。返回指定位置的item的id,即點擊位置位置的item的行id。(其實這個id和你傳到Adapter的數據集的相對應的順序id一致。)

方法View getView(final int position, View convertView, ViewGroup parent)的源碼:

 /**
     * Get a View that displays the data at the specified position in the data set. You can either
     * create a View manually or inflate it from an XML layout file. When the View is inflated, the
     * parent View (GridView, ListView...) will apply default layout parameters unless you use
     * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     * 
     * @param position The position of the item within the adapter's data set of the item whose view
     *        we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *        is non-null and of an appropriate type before using. If it is not possible to convert
     *        this view to display the correct data, this method can create a new view.
     *        Heterogeneous lists can specify their number of view types, so that this View is
     *        always of the right type (see {@link #getViewTypeCount()} and
     *        {@link #getItemViewType(int)}).
     * @param parent The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
     */
    View getView(int position, View convertView, ViewGroup parent);

大概意思是:得到顯示的數據集的指定位置的數據視圖。你也可以手動創建一個視圖(即顯示在屏幕上,這裏不知道翻譯成什麼好)或者填充一個xml佈局文件。當視圖被填充,父視圖將會應用默認的佈局參數,除非你使用方法inflate(int,android.view.ViewGroup,boolean)來指定一個根視圖並防止附加到根視圖上。

                    position參數解釋:在視圖中我們想要的適配器的數據集的item的位置。

                    convertView參數解釋:如果可能的話,舊的視圖重用。注意:你應該在使用之前檢查這個視圖是非空的並且是合適的類型。如果無法將此視圖轉換爲顯示正確的數據,則此方法可以創建新視圖。異構列表可以指定其視圖類型的數量,以便此視圖總是正確的類型(請參閱{@link #getViewTypeCount()}和{@link #getItemViewType(int)})。

                    parent參數解釋:此視圖將會被附加到父視圖。

                    返回值:返回一個與指定位置數據相對應的視圖。(即我們填充視圖)



總結一下:

1   以上的position參數都是一樣,都爲點擊的item的id,只不過方法返回的值不一樣,一個返回數據集即一個對象,一個返回id。

2   在get View()方法中最好檢查一下舊的視圖是否爲空。然後再決定是否創建新視圖。

    這裏的舊視圖解釋一下:在初始顯示的時候,每次顯示一個item都調用一次getview方法但是每次調用的時候convertview爲空(因爲還沒有舊的view),當顯示完了之後。如果屏幕移動了之後,並且導致有些Item(也可以說是view)跑到屏幕外面,此時如果還有新的item需要產生,則這些item顯示時調用的getview方法中的convertview參數就不是null,而是那些移出屏幕的view(舊view),我們所要做的就是將需要顯示的item填充到這些回收的view(舊view)中去,最後注意convertview爲null的不僅僅是初始顯示的那些item,還有一些是已經開始移入屏幕但是還沒有view被回收的那些item。

這裏詳細的converView參數解析可以看這篇博文,解析的很好,也涉及了對converview數據的更新操作。

    鏈接:http://home.bdqn.cn/thread-54626-1-1.html



接下來是點擊事件:

這個直接在getview中處理就行了,

 view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Context, xxx.class); 
                .........................................
                mContext.startActivity(intent);
            }
        });

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