一. ViewPager各個(gè)適配器

1. ViewPager的簡單適配器PageAdapter

參看文章第一二小節(jié)
實(shí)現(xiàn)PageAdaper必須實(shí)現(xiàn)它的4個(gè)方法

getCount()
返回頁面頁面數(shù)量

Object instantiateItem(ViewGroup container, int position)
將頁面的view加入container中, 并返回對(duì)應(yīng)的的key

boolean isViewFromObject(View view, Object object)
判斷頁面的view和key是否匹配

void destroyItem(ViewGroup container, int position, Object object)
將第position頁的view從container中移除

2.使用FragmentPageAdapter

實(shí)現(xiàn)兩個(gè)方法 (FragmentPageAdapter實(shí)現(xiàn)了instantiateItem,isViewFromObject,destroyItem)

public int getCount()
返回頁面頁面數(shù)量

Fragment getItem(int position)
返回第position頁的Fragment

注意 如下FragmentPagerAdapter的源碼

  1. instantitatItem首先從FragmentManager中獲取Fragment
    如果沒有吴侦,則再從getItem中獲取
  public Object instantiateItem(ViewGroup container, int position) {
        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }

        final long itemId = getItemId(position);

        // Do we already have this fragment?
        String name = makeFragmentName(container.getId(), itemId);
        Fragment fragment = mFragmentManager.findFragmentByTag(name);
        if (fragment != null) {
            if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
            mCurTransaction.attach(fragment);
        } else {
            fragment = getItem(position);
            if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
            mCurTransaction.add(container.getId(), fragment,
                    makeFragmentName(container.getId(), itemId));
        }
        if (fragment != mCurrentPrimaryItem) {
            fragment.setMenuVisibility(false);
            fragment.setUserVisibleHint(false);
        }

        return fragment;
    }

2 . destroyItem
從FragmentMananger detach掉Fragment,此時(shí)Fragment仍然在FragmentManager中

 public void destroyItem(ViewGroup container, int position, Object object) {
       if (mCurTransaction == null) {
           mCurTransaction = mFragmentManager.beginTransaction();
       }
       if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object
               + " v=" + ((Fragment)object).getView());
       mCurTransaction.detach((Fragment)object);
   }

3. FragmentPagerStateAdapter

仍然只需要實(shí)現(xiàn) getCount() 和instantiateItem()方法

FragmentPagerStateAdapter相關(guān)源碼

  1. instantitatItem 邏輯和FramentPagerAapter類似
    首先從FragmentManager中獲取Fragment
    如果沒有屋休,則再從getItem中獲取
  public Object instantiateItem(ViewGroup container, int position) {
       // If we already have this item instantiated, there is nothing
       // to do.  This can happen when we are restoring the entire pager
       // from its saved state, where the fragment manager has already
       // taken care of restoring the fragments we previously had instantiated.
       if (mFragments.size() > position) {
           Fragment f = mFragments.get(position);
           if (f != null) {
               return f;
           }
       }

       if (mCurTransaction == null) {
           mCurTransaction = mFragmentManager.beginTransaction();
       }

       Fragment fragment = getItem(position);
       if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
       if (mSavedState.size() > position) {
           Fragment.SavedState fss = mSavedState.get(position);
           if (fss != null) {
               fragment.setInitialSavedState(fss);
           }
       }
       while (mFragments.size() <= position) {
           mFragments.add(null);
       }
       fragment.setMenuVisibility(false);
       fragment.setUserVisibleHint(false);
       mFragments.set(position, fragment);
       mCurTransaction.add(container.getId(), fragment);

       return fragment;
   }

2 . destroyItem
從FragmentMananger remove掉Fragment,此時(shí)Fragment不在FragmentManager中

   @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        Fragment fragment = (Fragment) object;

        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }
        if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object
                + " v=" + ((Fragment)object).getView());
        while (mSavedState.size() <= position) {
            mSavedState.add(null);
        }
        mSavedState.set(position, fragment.isAdded()
                ? mFragmentManager.saveFragmentInstanceState(fragment) : null);
        mFragments.set(position, null);

        mCurTransaction.remove(fragment);
    }
  1. FragmentPagerAdapter與FragmentPagerStateAdapter的區(qū)別


    image

實(shí)例

  1. viewpager.setOffscrennPageLimit(1);
  2. 從第一頁滑動(dòng)第二頁,會(huì)創(chuàng)建第三頁
  3. 從第二頁滑到第一頁备韧,會(huì)調(diào)用destroyItem摧毀第三頁
  4. 區(qū)別在于FragmentPagerAdapter與FragmentPagerStateAdapter的destoryItem方法實(shí)現(xiàn)不一樣劫樟。FragmentPagerAdapter會(huì)將Fragment從FragmentManger總detach()
    FragmentPagerStateAdapter會(huì)將Fragment從FragmentManger總remove()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市盯蝴,隨后出現(xiàn)的幾起案子毅哗,更是在濱河造成了極大的恐慌,老刑警劉巖捧挺,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件虑绵,死亡現(xiàn)場離奇詭異,居然都是意外死亡闽烙,警方通過查閱死者的電腦和手機(jī)翅睛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來黑竞,“玉大人捕发,你說我怎么就攤上這事『芑辏” “怎么了扎酷?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長遏匆。 經(jīng)常有香客問我法挨,道長,這世上最難降的妖魔是什么幅聘? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任凡纳,我火速辦了婚禮,結(jié)果婚禮上帝蒿,老公的妹妹穿的比我還像新娘荐糜。我一直安慰自己,他們只是感情好葛超,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布暴氏。 她就那樣靜靜地躺著,像睡著了一般绣张。 火紅的嫁衣襯著肌膚如雪答渔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天胖替,我揣著相機(jī)與錄音,去河邊找鬼。 笑死独令,一個(gè)胖子當(dāng)著我的面吹牛端朵,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播燃箭,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼冲呢,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了招狸?” 一聲冷哼從身側(cè)響起敬拓,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎裙戏,沒想到半個(gè)月后乘凸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡累榜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年营勤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片壹罚。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡葛作,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出猖凛,到底是詐尸還是另有隱情赂蠢,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布辨泳,位于F島的核電站虱岂,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏漠吻。R本人自食惡果不足惜量瓜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望途乃。 院中可真熱鬧绍傲,春花似錦、人聲如沸耍共。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽试读。三九已至杠纵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間钩骇,已是汗流浹背比藻。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國打工铝量, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人银亲。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓慢叨,卻偏偏與公主長得像,于是被迫代替她去往敵國和親务蝠。 傳聞我的和親對(duì)象是個(gè)殘疾皇子拍谐,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容