最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

安卓開(kāi)發(fā)之FragmentPagerAdapter和FragmentStatePagerAdapter詳解

 更新時(shí)間:2022年08月22日 08:37:32   作者:?jiǎn)躺? 
這篇文章主要介紹了安卓開(kāi)發(fā)之FragmentPagerAdapter和FragmentStatePagerAdapter詳解的相關(guān)資料,需要的朋友可以參考下

最近遇到比較奇怪的bug,TableLayout+ViewPager實(shí)現(xiàn)點(diǎn)擊頂部tab切換viewpager視圖。但是在Viewpager設(shè)置dapter時(shí),最開(kāi)始設(shè)置的是FragmentPagerAdapter,會(huì)導(dǎo)致tab切換后FragmentPagerAdapter內(nèi)的視圖未刷新(與上一個(gè)tab內(nèi)容重復(fù)或展示成空白,展示成空白一般出現(xiàn)在頁(yè)面重啟后不能完成刷新成功)。替換成FragmentStatePagerAdapter或者FragmentStateAdapter,便解決了這一問(wèn)題。這其實(shí)是個(gè)比較常見(jiàn)的bug,網(wǎng)絡(luò)上有很多推薦的解決方案。那么到底FragmentPagerAdapter、FragmentStateAdapter以及FragmentStatePagerAdapter有何具體的區(qū)別呢?在這篇文章中我將詳細(xì)解答。
根據(jù)類圖進(jìn)行分析

FragmentPagerAdapter與FragmentPagerStateAdapter區(qū)別點(diǎn):

一:二者在狀態(tài)保存有差異:FragmentPagerAdapter并未實(shí)現(xiàn)saveState()、restoreState()

public class FragmentPagerAdapter{
? ? // ......
? ? public static final int POSITION_UNCHANGED = -1;
? ? public static final int POSITION_NONE = -2;
?
? ? public Parcelable saveState() {
? ? ? ? return null;
? ? }
?
? ? public void restoreState(Parcelable state, ClassLoader loader) {
? ? ? ??
? ? }
}

而FragmentPagerStateAdapter則實(shí)現(xiàn)了saveState()、restoreState()這倆方法:

?public Parcelable saveState() {
? ? ? ? Bundle state = null;
? ? ? ? if (mSavedState.size() > 0) {
? ? ? ? ? ? state = new Bundle();
? ? ? ? ? ? Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
? ? ? ? ? ? mSavedState.toArray(fss);
? ? ? ? ? ? state.putParcelableArray("states", fss);
? ? ? ? }
? ? ? ? for (int i=0; i<mFragments.size(); i++) {
? ? ? ? ? ? Fragment f = mFragments.get(i);
? ? ? ? ? ? if (f != null && f.isAdded()) {
? ? ? ? ? ? ? ? if (state == null) {
? ? ? ? ? ? ? ? ? ? state = new Bundle();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? String key = "f" + i;
? ? ? ? ? ? ? ? mFragmentManager.putFragment(state, key, f);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return state;
? ? }
?
? ? @Override
? ? public void restoreState(Parcelable state, ClassLoader loader) {
? ? ? ? if (state != null) {
? ? ? ? ? ? Bundle bundle = (Bundle)state;
? ? ? ? ? ? bundle.setClassLoader(loader);
? ? ? ? ? ? Parcelable[] fss = bundle.getParcelableArray("states");
? ? ? ? ? ? mSavedState.clear();
? ? ? ? ? ? mFragments.clear();
? ? ? ? ? ? if (fss != null) {
? ? ? ? ? ? ? ? for (int i=0; i<fss.length; i++) {
? ? ? ? ? ? ? ? ? ? mSavedState.add((Fragment.SavedState)fss[i]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Iterable<String> keys = bundle.keySet();
? ? ? ? ? ? for (String key: keys) {
? ? ? ? ? ? ? ? if (key.startsWith("f")) {
? ? ? ? ? ? ? ? ? ? int index = Integer.parseInt(key.substring(1));
? ? ? ? ? ? ? ? ? ? Fragment f = mFragmentManager.getFragment(bundle, key);
? ? ? ? ? ? ? ? ? ? if (f != null) {
? ? ? ? ? ? ? ? ? ? ? ? while (mFragments.size() <= index) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? mFragments.add(null);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? f.setMenuVisibility(false);
? ? ? ? ? ? ? ? ? ? ? ? mFragments.set(index, f);
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? Log.w(TAG, "Bad fragment at key " + key);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }

FragmentStatePagerAdapter對(duì)Fragment的狀態(tài)進(jìn)行了保存

二:二者在視圖管理方法差異:

FragmentStatePagerAdapter是整個(gè)Fragment對(duì)象的移除和重建

?public Object instantiateItem(ViewGroup container, int position) {
? ? ? ? if (mFragments.size() > position) {
? ? ? ? ? ? Fragment f = mFragments.get(position);
? ? ? ? ? ? if (f != null) {
? ? ? ? ? ? ? ? return f;
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? if (mCurTransaction == null) {
? ? ? ? ? ? mCurTransaction = mFragmentManager.beginTransaction();
? ? ? ? }
?
? ? ? ? // 實(shí)例化fragment(交給我們實(shí)現(xiàn)的getItem方法)
? ? ? ? Fragment fragment = getItem(position);
?
? ? ? ? if (mSavedState.size() > position) {
? ? ? ? ? ? Fragment.SavedState fss = mSavedState.get(position);
? ? ? ? ? ? if (fss != null) {
? ? ? ? ? ? ? ? fragment.setInitialSavedState(fss);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 如果緩存 <= ViewPager傳入的position,說(shuō)明當(dāng)前位置還未存入緩存.
? ? ? ? while (mFragments.size() <= position) {
? ? ? ? ? ? // 先占個(gè)坑
? ? ? ? ? ? mFragments.add(null);
? ? ? ? }
? ? ? ? fragment.setUserVisibleHint(false);
? ? ? ? // 填坑
? ? ? ? mFragments.set(position, fragment);
? ? ? ? // 填充視圖
? ? ? ? mCurTransaction.add(container.getId(), fragment);
? ? ? ? return fragment;
? ? }
?
? ? @Override
? ? public void destroyItem(ViewGroup container, int position, Object object) {
? ? ? ? Fragment fragment = (Fragment) object;
?
? ? ? ? if (mCurTransaction == null) {
? ? ? ? ? ? mCurTransaction = mFragmentManager.beginTransaction();
? ? ? ? }
? ? ? ? // 從緩存中移除
? ? ? ? mFragments.set(position, null);
? ? ? ? // 從FragmentManager中移除
? ? ? ? mCurTransaction.remove(fragment);
? ? }

FragmentPagerAdapter是視圖的attach和detach,不會(huì)對(duì)整個(gè)fragment進(jìn)行完全的添加和刪除操作。

因此,可見(jiàn)二者在使用場(chǎng)景上不同,如果頁(yè)面較少,仍舊希望能夠?qū)⑸傻腇ragment保存在內(nèi)存中,在需要顯示的時(shí)候直接調(diào)用。而不要產(chǎn)生生成、銷毀對(duì)象的額外開(kāi)銷。這樣效率最高。這種情況下,選中FragmentPagerAdapter更合適。

對(duì)于在使用FragmentPagerAdapter出現(xiàn)白屏或者刷新不了的bug,除了替換成FragmentStatePagerAdapter,還需要重載getItem()和instantiateItem()對(duì)象。

對(duì)于getItemPosition()方法,兩個(gè)累的區(qū)別是:FragmentStatePagerAdapter會(huì)在因POSITION_NONE觸發(fā)調(diào)用的destroyItem中真正的釋放資源,重新建立一個(gè)新的Fragment;而FragmentPagerAdapter僅僅會(huì)在destoryItem()中detach這個(gè)Fragment,在instantiateItem()時(shí)會(huì)使用舊的Fragment,并觸發(fā)attach,并沒(méi)有觸發(fā)資源及重建的過(guò)程。

到此這篇關(guān)于安卓開(kāi)發(fā)之FragmentPagerAdapter和FragmentStatePagerAdapter詳解的文章就介紹到這了,更多相關(guān)FragmentPagerAdapter和FragmentStatePagerAdapter詳解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

昌都县| 凯里市| 农安县| 南召县| 郑州市| 本溪市| 石楼县| 八宿县| 沂南县| 绿春县| 巫溪县| 沧源| 松桃| 南部县| 垣曲县| 健康| 喀什市| 无锡市| 冷水江市| 磐石市| 孟州市| 雷山县| 安图县| 常宁市| 永吉县| 土默特右旗| 彭州市| 峨眉山市| 琼结县| 正宁县| 肃宁县| 宁城县| 武强县| 清苑县| 天气| 迁西县| 阿拉善右旗| 武义县| 九台市| 沿河| 什邡市|