欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Adapter的幾個(gè)常用方法

 更新時(shí)間:2016年09月12日 08:59:52   作者:willhua  
這篇文章主要為大家詳細(xì)介紹了Android Adapter的幾個(gè)常用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Android Adapter的幾個(gè)常用方法分享給大家,具體內(nèi)容如下

1  ListView是在什么時(shí)候設(shè)置對Adapter的數(shù)據(jù)監(jiān)聽的? 

在setAdapter(ListAdapter adapter)中,會先取消ListView中原來的mAdapter中的數(shù)據(jù)監(jiān)聽(mAdapter.unregisterDataSetObserver(mDataSetObserver);),然后再設(shè)置對新設(shè)置的adapter的數(shù)據(jù)監(jiān)聽。 

2  getView(int position, View convertView, ViewGroup parent) 

我們都知道m(xù)Adapter的getView方法很重要,那么該方法在ListView是怎么被利用的呢? 在ListView的源碼中沒有發(fā)現(xiàn)getView方法的調(diào)用,于是我們?nèi)istView的父類AbsListView。在AbsListView中的obtainView中調(diào)用了getView,其主要代碼邏輯部分為:

View obtainView(int position, boolean[] isScrap) {
    isScrap[0] = false;
    View scrapView;
    //從回收器中獲取view
    scrapView = mRecycler.getScrapView(position);

    View child;
    if (scrapView != null) {
      ...
      //若不為空,則傳入convertView,這樣的話重用了view,同時(shí)更新了數(shù)據(jù)
      child = mAdapter.getView(position, scrapView, this);
      ...
    } else {
      //若為空,則在getView中重新創(chuàng)建HolderView,且填入數(shù)據(jù)
      child = mAdapter.getView(position, null, this);
      ...
    }
    return child;
  }

而obtainView又會在ListView的measure以及生成整個(gè)ListView等中用到。 

對于重寫getView方法最終要的應(yīng)該就是要記得convertView的重用了,沒有重用幾乎都會造成內(nèi)存卸了。 

3  getCount() 

Adapter的getCount()用來干啥?  在ListView中,在onMeasure以及觸控分發(fā)響應(yīng)等過程中都會用到Adapter的getCount()函數(shù)。毫無疑問的是:它應(yīng)該返回底層數(shù)據(jù)的數(shù)據(jù)個(gè)數(shù)。 

4  getItem(int position) 

getItem()在AdapterView中被調(diào)用,然后供用戶調(diào)用:從這兩個(gè)函數(shù)的描述我們可以看出,我們應(yīng)該在Adapter的getItem()方法中返回position對應(yīng)的數(shù)據(jù),但是不是說一定要返回用于在Item的View上展示的數(shù)據(jù),這個(gè)還是看需求,雖然可能大部分情況都是返回View中展示的數(shù)據(jù)。 

  /**
   * Gets the data associated with the specified position in the list.
   *
   * @param position Which data to get
   * @return The data associated with the specified position in the list
   */
  public Object getItemAtPosition(int position) {
    T adapter = getAdapter();
    return (adapter == null || position < 0) ? null : adapter.getItem(position);
  }
  
  /**
   * @return The data corresponding to the currently selected item, or
   * null if there is nothing selected.
   */
  public Object getSelectedItem() {
    T adapter = getAdapter();
    int selection = getSelectedItemPosition();
    if (adapter != null && adapter.getCount() > 0 && selection >= 0) {
      return adapter.getItem(selection);
    } else {
      return null;
    }
  }

縱觀整個(gè)結(jié)構(gòu),可以說存在這樣的三層:dataLists(原底層數(shù)據(jù))--Adapter--AdapterView,有了getItem()方法的存在,我們可以直接利用Adapter來獲取數(shù)據(jù),而不需要獲取底層dataLists的引用;有了getItemAtPosition()方法的存在,我們可以直接利用AdapterView 獲取底層數(shù)據(jù),而不需要獲取其Adapter的引用。這樣的話,對于編程的簡便性以及解耦性都好很多。

5    getItemId(int position) 

在AdapterView中發(fā)現(xiàn)它的一些調(diào)用,

public long getItemIdAtPosition(int position) {
    T adapter = getAdapter();
    return (adapter == null || position < 0) ? INVALID_ROW_ID : adapter.getItemId(position);
  }
  
  
  private void fireOnSelected() {
    if (mOnItemSelectedListener == null)
      return;

    int selection = this.getSelectedItemPosition();
    if (selection >= 0) {
      View v = getSelectedView();
      //這里調(diào)用的getItemId得到的返回值與selection都屬于同一個(gè)item的特征,其意義也就在于在選擇接口的onItemSelected方法中可
      //以直接拿到該item的id,而不需要通過獲取adapter來間接實(shí)現(xiàn)
      mOnItemSelectedListener.onItemSelected(this, v, selection,
          getAdapter().getItemId(selection));
    } else {
      mOnItemSelectedListener.onNothingSelected(this);
    }
  }
  
  int findSyncPosition() {
    ...
      rowId = adapter.getItemId(seed);
      if (rowId == idToMatch) { //從這里來看,getItemId似乎應(yīng)該對于不同的item返回不同的值,保持唯一性
        // Found it!
        return seed;
      }
    ...
  }

與上面分析的getItem()方法一樣,getItemId()和getItemIdAtPosition()都提供了編程上面的便利。但是目前來看,由于對id沒啥需求,所以大部分在重寫getItemId方法時(shí)都是直接返回的position值,這樣做也是對的,雖然從數(shù)據(jù)獲取上沒啥意義(我給你一個(gè)position,你原封不動的返回給我,啥意思)。但是我想說明的是,不要被這個(gè)做法所限制,而以為ItemId就是item在數(shù)據(jù)中的position。其實(shí)若有需求,可以利用getItemId()方法返回一些其他的值,比如每個(gè)item數(shù)據(jù)在數(shù)據(jù)庫中id值,或者每個(gè)人的身份證號等。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論