fragment的基本用法

這篇文章主要講解了“fragment的基本用法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“fragment的基本用法”吧!

公司主營(yíng)業(yè)務(wù):成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出察布查爾錫伯免費(fèi)做網(wǎng)站回饋大家。

fragment 基本使用方法
    1、繼承Fragment類或者子類并重寫
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState)
       方法。把xml文件包裹成View對(duì)象,并對(duì)vie進(jìn)行操作,與activity操作View相同。
    2、把fragment添加到activity中:
      1)、在xml中添加,也即靜態(tài)添加:
              <fragment
            android:layout_below="@id/tv"
            android:id="@+id/left_fragment"
            android:name="com.example.test.LeftFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
       2)、在代碼中添加,也即動(dòng)態(tài)添加:
            FragmentManager LeftFragment fragment=new LeftFragment();
            fragmentManager=getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.ll, fragment);
            fragmentTransaction.commit();
            注:需要commit().
    3、基本操作,增、刪、改、查。
         重要類:
           FragmentManager: Fragment管理
           FragmentTransaction:Fragment事務(wù)
           Fragment:具體Fragment
         增:add方法
         刪:remove方法
         改:replace方法
         查:findFragmentByTag 或者 findFragmentById
    4、回退棧
         使用FragmentTransaction中的addToBackStack方法
    5、與activity的通訊
           在Fragment內(nèi)定義一個(gè)接口,其作用是讓activity使用。
        1)、
            public class FragmentB_1 extends Fragment implements OnClickListener {
                private FragmentBIF fragmentBIF;
                public interface FragmentBIF{
                    public void setB();
                }
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                    View view = inflater.inflate(R.layout.b, null);
                    Button button=(Button) view.findViewById(R.id.but_b);
                    button.setOnClickListener(this);
                    return view;
                }
                public void setFragmentBIF(FragmentBIF fragmentBIF) {
                    this.fragmentBIF = fragmentBIF;
                }
                //button的點(diǎn)擊事件作用到接口的setB方法
                @Override
                public void onClick(View v) {
                    if(fragmentBIF!=null){
                        fragmentBIF.setB();
                    }
                }
            }
         2)、
            public class FragmentA_1 extends Fragment implements OnClickListener {
            
                private FragmentIF fragmentIF;
                public interface FragmentIF{
                    public void setA();
                }
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                     View view = inflater.inflate(R.layout.a, null);
                     Button button = (Button) view.findViewById(R.id.but_a);
                     button.setOnClickListener(this);
                     return view;
                }
                
                //button的點(diǎn)擊事件作用到接口的setA方法上
                @Override
                public void onClick(View v) {
                    if(getActivity() instanceof FragmentIF){
                        ((FragmentIF)getActivity()).setA();
                    }
                }
            }
                 

感謝各位的閱讀,以上就是“fragment的基本用法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)fragment的基本用法這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

文章題目:fragment的基本用法
文章路徑:http://muchs.cn/article34/jehdse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、網(wǎng)站策劃搜索引擎優(yōu)化、手機(jī)網(wǎng)站建設(shè)品牌網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司