【6】Fragment

一、什么是Fragment

fragment可以看做是一個依附于activity存在的布局,他必須在activity中被調(diào)用,不可單獨(dú)出現(xiàn)(其生命周期也與activity一一對應(yīng)),主要用來實(shí)現(xiàn)不同尺寸安卓設(shè)備的碎片化問題,可以讓同一個應(yīng)用在不同設(shè)備上有不同的顯示效果。

二、Fragment的生命周期

和activity的生命周期一一對應(yīng)。

需要注意的是:fragment在創(chuàng)建時后于其依附的activity被創(chuàng)建,在銷毀時先于其依附的activity被銷毀。

1、創(chuàng)建時:

  • onAttach()
  • onCreate()
  • onCreateView()
  • onActivityCreated()

2、對用戶可見:

  • onstart()
  • onResume()

3、進(jìn)入后臺模式時:

  • onPause()
  • onstop()

4、退出時:

  • onPause()
  • onStop()
  • onDestroyView()
  • onDestroy()
  • onDetach()

三、Fragment的加載方式

1、靜態(tài)加載

即直接將fragment的類名寫在activity的布局文件中,通常需要用在onCreateView方法中用inflater方法將布局轉(zhuǎn)換為view,再將其返回給activity調(diào)用。

@Override

publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //將布局文件轉(zhuǎn)化為一個view
    
    //需要加載的布局,父view,是否加入到父view中
    
    View view = inflater.inflate(R.layout.fragment,container,false);
    
    TextView textView = (TextView) view.findViewById(R.id.fragment_text);
    
    textView.setText("靜態(tài)加載fragment");
    
    Button button = (Button) view.findViewById(R.id.fragment_button);
    
    button.setText("獲取數(shù)據(jù)");
    
    button.setOnClickListener(newView.OnClickListener() {
    
    @Override
    
    public voidonClick(View view) {
    
    String text = getAaa();
    
    Toast.makeText(getActivity(),"text:"+text, Toast.LENGTH_SHORT).show();
    
    }
    
    });
    
    returnview;

}

2、動態(tài)加載

即使用fragmentManager來動態(tài)加載到activity的某個布局中。

Fragment fragment2 =newMyFragment2();

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

//第一個參數(shù):需要加入的布局

//第二個參數(shù):需要加入的fragment

fragmentTransaction.add(R.id.frame,fragment2);

fragmentTransaction.addToBackStack(null);//添加返回上一層的事務(wù),否則按返回直接退出程序。

fragmentTransaction.commit();//一定要在最后執(zhí)行commit()事務(wù),執(zhí)行這個操作。

四、Fragment與Activity之間的消息傳遞

1、activity向fragement傳遞消息

通過setArguments(bundle)和getArguments()方法即可。

activity中

Fragment fragment5 =newMyFragment5();

Bundle bundle =newBundle();

bundle.putString("name",text);

fragment5.setArguments(bundle);

fragment5中

text= getArguments().get("name")+"";

也可通過fragmentManager.findFragmentById(R.id.main4_fragment)的方式來靜態(tài)傳值。用的比較少,在此不詳述。

2、fragement向activity傳遞消息

一般需要通過一個回調(diào)方法來傳遞消息。

fragment中

privateStringcode="Thank you,Activity!";

privateMyListenerlistener;

public interfaceMyListener{//此為回調(diào)接口

    public voidthink(String code);

}
@Override

public voidonAttach(Context context) {

    listener= (MyListener) context;
    
    super.onAttach(context);

}
publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //將布局文件轉(zhuǎn)化為一個view
    
    View view = inflater.inflate(R.layout.fragment2,container,false);
    
    
    
    listener.think(code);//在初始化時執(zhí)行回調(diào)方法。
    
    returnview;

}

activity中

實(shí)現(xiàn)fragment中的接口即可獲得消息。

public class MainActivity4 extends Activity implements MyFragment5.MyListener{

    public void think(String code) {
    
    String code = code;
    
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容