前言
fragment 可認(rèn)為是一個(gè)輕量級(jí)的Activity,但不同與Activity,它是要嵌到Activity中來(lái)使用的,它用來(lái)解決設(shè)備屏幕大小的不同,主要是充分利用界面上的空間,如平板上多余的空間。一個(gè)Activity可以插入多個(gè)Fragment,可以認(rèn)為Fragment就是Activity上的一個(gè)View。

本文主要介紹了關(guān)于Android中Fragment的基本用法,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
一、fragment管理
在activity動(dòng)態(tài)加載fragment
加載fragment的布局不限,并不局限于FrameLayout
加載方法:(只有導(dǎo)入的fragment是v4包,才有g(shù)etSupportFragmentManager())
FragmentA fragment = new FragmentA(); getSupportFragmentManager().beginTransaction() .add(R.id.XXX, fragment, "fragment")// .addToBackStack("") //加入回退棧 .commit;或者
FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.fragment_container, fragment, "fragment"); transaction.commit();
但是第二種方法要注意,導(dǎo)包須一致
import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;
而除了使用add方法,我們也可以使用replace方法添加fragment
FragmentA fragment = new FragmentA();getSupportFragmentManager().beginTransaction() .replace(R.id.XXX, fragment,"fragment") .commit();
add和replace的區(qū)別是:
①add 是往container容器里堆加fragment View;replace是將container容器里之前添加的View全部清除,然后再添加當(dāng)前fragment View
一定要記住,replace清除的是container的視圖,而不是fragment實(shí)例, remove移除的才是fragment實(shí)例
②add后期可以使用show,hide操作,但是replace不可以,原因見(jiàn)①
③使用add,回滾時(shí),fragment不會(huì)重新加載,曾經(jīng)的操作痕跡還存在,使用replace回滾時(shí),之前的fragment會(huì)重新加載,原因見(jiàn)①
使用add的時(shí)候還有一點(diǎn)需要注意的是,視圖重疊的問(wèn)題,記得設(shè)置背景色
add 和 replace 千萬(wàn)不要混合使用,否則會(huì)出錯(cuò)
在進(jìn)行remove,hide,show之前要記得判斷fragment.isAdded();下面的代碼我就不提示了
移除fragment實(shí)例
Fragment fragment = getSupportFragmentManager().findFragmentByTag("fragment"); getSupportFragmentManager().beginTransaction() .remove(fragment) .commit();隱藏fragment
Fragment fragment = getSupportFragmentManager().findFragmentByTag("fragment"); getSupportFragmentManager().beginTransaction() .hide(fragment) .commit();顯示fragment
Fragment fragment = getSupportFragmentManager().findFragmentByTag("fragment"); getSupportFragmentManager().beginTransaction() .show(fragment) .commit();拓展:
在fragment里刷新(即從頭加載fragment數(shù)據(jù),且不影響后續(xù)的回退棧)
Fragment replaceFragment = getActivity().getSupportFragmentManager().findFragmentByTag("first_fragment"); getActivity().getSupportFragmentManager().beginTransaction() .detach(replaceFragment) .attach(replaceFragment) .commit();簡(jiǎn)單的講,detach是銷(xiāo)毀View,而不是fragment實(shí)例,attach是重建視圖View,attach后的視圖會(huì)位于視圖最前面,具體的可以自己去查看資料
二、回滾操作
最常用:依次回滾
@Override public void onBackPressed() { if (getSupportFragmentManager().getBackStackEntryCount() <= 1) { //這里是取出我們返回棧存在Fragment的個(gè)數(shù) finish(); } else { //取出我們返回棧保存的Fragment,這里會(huì)從棧頂開(kāi)始彈棧 getSupportFragmentManager().popBackStack(); } }拓展:指定回滾
void popBackStack(String name, int flags);
參數(shù)string name是transaction.addToBackStack(String tag)中的tag值;
至于int flags有兩個(gè)取值:0或FragmentManager.POP_BACK_STACK_INCLUSIVE;
當(dāng)取值0時(shí),表示除了參數(shù)一指定這一層之上的所有層都退出棧,指定的這一層為棧頂層;
當(dāng)取值POP_BACK_STACK_INCLUSIVE時(shí),表示連著參數(shù)一指定的這一層一起退出棧;
退回棧頂:
while (getSupportFragmentManager().getBackStackEntryCount()>1) { getSupportFragmentManager().popBackStackImmediate();}三、重點(diǎn)來(lái)了,在fragment里面加載fragment 顯示問(wèn)題
1.顯示不出來(lái):
加載fragment的布局不要使用LinearLayout,不要使用LinearLayout,不要使用LinearLayout,重要的事情說(shuō)三遍,否則很有可能加載的fragment顯示不出來(lái)
2.顯示不完全:在onCreateView使用方式一,不要使用方式二
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 方式一 View view = inflater.inflate(R.layout.XXX, container, false); // 方式二// View view = inflater.inflate(R.layout.XXX, null); return view; }3.點(diǎn)擊事件透?jìng)?/strong>
①如果加載的fragment的布局為ScrollView,不會(huì)發(fā)生透?jìng)魇录?/p>
②如果存在透?jìng)魇录趂ragment的根布局加上android:clickable="true" ,即可簡(jiǎn)單粗暴的解決點(diǎn)擊事件穿透的問(wèn)題
4.獲取回退棧中fragment的數(shù)量
方式一:activity如繼承FragmentActivity,可通過(guò)getSupportFragmentManager().getBackStackEntryCount()判斷activity中棧內(nèi)已存的fragment的數(shù)量,不包括通過(guò)方式二加載進(jìn)去的fragment(在fragment中加載子fragment)
方式二:此方式是在fragment中通過(guò)getChildFragmentManager().getBackStackEntryCount()判斷此fragment棧內(nèi)已存的fragment的數(shù)量
getChildFragmentManager().beginTransaction() .replace(R.id.XXX, fragment) .addToBackStack(null) .commit();
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選