1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      Android中Fragment的加載方式與數(shù)據(jù)通信詳解

      一、加載方式

      網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了奎文免費(fèi)建站歡迎大家使用!

      1. 靜態(tài)加載

      1.1 加載步驟

      (1) 創(chuàng)建fragment:創(chuàng)建自定義Fragment類繼承自Fragment類,同時(shí)將自定義Fragment類與Fragment視圖綁定(將layout轉(zhuǎn)換成View)

      View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

      inflater用于綁定Fragment的布局文件,同時(shí)將該布局轉(zhuǎn)換成View對象并返回;container為Fragment的UI所在的父容器。返回值為Fragment顯示的UI,若不顯示,則返回null。

      inflate(int resource, ViewGroup root, boolean attachToRoot)

      resource為Fragment需要加載的布局文件;root為加載Fragment的父ViewGroup,也就是onCreateView傳遞進(jìn)來的container;attachToRoot為是否返回父ViewGroup。

      (2) 使用fragment:在父視圖中引入fragment,靜態(tài)加載必須指定name屬性以及一個(gè)唯一標(biāo)識符,標(biāo)識符可以為id或者tag

      
      android:name
      
      android:id
      android:tag

      (3) 監(jiān)聽事件:若在父視圖對應(yīng)的類中設(shè)置監(jiān)聽事件,可以直接訪問fragment中的子組件;若在Fragment的類中設(shè)置,則必須通過inflate()返回的View對象訪問Fragment中的子組件(view.findViewById(id))。

      1.2 簡單范例

      MyFragment視圖:

      <?xml version="1.0" encoding="utf-8"?>
      
       
      

      MyFragment類:

      public class MyFragment extends Fragment {
       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       //將layout布局轉(zhuǎn)換成View對象
       View view = inflater.inflate(R.layout.myfragment, container, false);
       //必須通過view對象對其子組件進(jìn)行訪問
       TextView textView = (TextView) view.findViewById(R.id.fragment_text);
       textView.setText("這里是fragment");
       //返回Fragment顯示UI
       return view;
       }
      }
      

      引用fragment的父視圖:

      <?xml version="1.0" encoding="utf-8"?>
      
       
      

      父視圖對應(yīng)的類設(shè)置事件監(jiān)聽:

      public class StaticFragmentActivity extends Activity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_static_fragment);
       //可直接通過findViewById訪問
       findViewById(R.id.fragment_text).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Toast.makeText(StaticFragmentActivity.this, "點(diǎn)擊了文本", Toast.LENGTH_SHORT).show();
        }
       });
       }
      }
      

      2. 動態(tài)加載

      2.1 加載步驟

      (1) 獲取事務(wù)管理器:對Fragment進(jìn)行的添加、移除、替換等操作,均為事務(wù)。需通過以下代碼獲取事務(wù)管理器,從而對fragment進(jìn)行動態(tài)操作。

      FragmentManager fm = getFragmentManager();
      FragmentTransaction ft = fm.beginTransaction();

      (2) 創(chuàng)建Fragment對象:創(chuàng)建需要加載的fragment,而后通過add或replace等方法實(shí)現(xiàn)動態(tài)加載。

      2.2 簡單范例

      布局:

      <?xml version="1.0" encoding="utf-8"?>
      
       

      Java:

      public class DynamicFragmentActivity extends Activity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_dynamic_fragment);
       findViewById(R.id.load).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //獲取事務(wù)管理器
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //創(chuàng)建fragment,并將其動態(tài)加載到id位container的布局中
        MyFragment myFragment = new MyFragment();
        fragmentTransaction.add(R.id.container, myFragment);
        //提交事務(wù)
        fragmentTransaction.commit();
        }
       });
       }
      }
      

      二、數(shù)據(jù)通信

      3. Activity向Fragment傳遞數(shù)據(jù)

      3.1 Activity向動態(tài)加載的Fragment傳遞數(shù)據(jù)

      (1)在Activity中獲取Fragment對象;

      (2)創(chuàng)建Bundle對象并傳入數(shù)據(jù);

      (3)將Bundle對象傳遞給Fragment對象;

      (4)在Fragment中獲取Bundle對象并拆包得到數(shù)據(jù)。

      范例:Activity中只有一個(gè)id為send的Button,MyFragment中只有一個(gè)TextView,這里就不再放布局代碼了。

      Activity:

      public class MainActivity extends Activity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //創(chuàng)建Fragment對象
        MyFragment myFragment = new MyFragment();
        //創(chuàng)建Bundle對象并傳入數(shù)據(jù)
        Bundle bundle = new Bundle();
        bundle.putString("info", "這里是向Fragment傳遞的數(shù)據(jù)");
        myFragment.setArguments(bundle);
        //加載Fragment
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
        beginTransaction.add(R.id.layout, myFragment, "myfragment");
        beginTransaction.commit();
        }
       });
       }
      }
      

      Fragment:

      public class MyFragment extends Fragment {
       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.my_fragment, container, false);
       TextView tv = (TextView) view.findViewById(R.id.text);
       //獲取數(shù)據(jù)
       String text = getArguments().get("info") + "";
       tv.setText(text);
       return view;
       }
      }
      

      3.2 Activity向靜態(tài)加載的Fragment傳遞數(shù)據(jù)

      (1)在Fragment中創(chuàng)建作為容器的數(shù)據(jù)對象,并創(chuàng)建getter和setter;

      (2)在Activity中獲取FragmentManager;

      (3)通過事務(wù)管理器的findFragmentById或findFragmentByTag方法,獲得fragment對象;

      (4)通過獲得的fragment對象調(diào)用容器的setter方法進(jìn)行傳值。

      范例:這里的布局與動態(tài)加載的布局唯一不同的就是將send按鈕放在了Fragment里面,其它相同。

      Fragment:

      public class MyFragment extends Fragment {
       private Button btn;
       private String received;//作為容器的對象
       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.my_fragment, container, false);
       TextView tv = (TextView) view.findViewById(R.id.text);
       tv.setText("這里是Fragment");
       btn = (Button) view.findViewById(R.id.send);
       btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Toast.makeText(getActivity(), "成功接收\"" + getReceived() + "\"", Toast.LENGTH_SHORT).show();
        }
       });
       return view;
       }
       public String getReceived() {
       return received;
       }
       public void setReceived(String received) {
       this.received = received;
       }
      }
      

      Activity:

      public class MainActivity extends Activity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       FragmentManager fragmentManager = getFragmentManager();
       MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.my_fragment);
       myFragment.setReceived("this is a test.");
       }
      }
      

      4. Fragment向Activity傳遞數(shù)據(jù)

      (1)在Fragment中寫一個(gè)回調(diào)接口;

      (2)在activity中實(shí)現(xiàn)這個(gè)回調(diào)接口,實(shí)現(xiàn)的函數(shù)用于傳值;

      (3)重寫Fragment中onAttach,在其中創(chuàng)建一個(gè)接口對象,得到傳遞過來的activity(我的理解是這個(gè)接口其實(shí)相當(dāng)于傳遞過來的activity的一個(gè)父類,這一步是用到了多態(tài)的特性);

      (4)用得到的接口對象進(jìn)行傳值。

      Fragment:

      public class MyFragment extends Fragment {
       private SendData sendData;
       @Override
       public void onAttach(Activity activity) {
       super.onAttach(activity);
       //獲取實(shí)現(xiàn)的接口對象
       sendData = (SendData) activity;
       }
       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.my_fragment, container, false);
       TextView tv = (TextView) view.findViewById(R.id.text);
       tv.setText("這里是Fragment");
       //通過接口對象傳遞數(shù)據(jù)
       sendData.sendMsg("this is a test.");
       return view;
       }
       //定義一個(gè)回調(diào)接口
       public interface SendData{
       void sendMsg(String str);
       }
      }
      

      Activity:

      public class MainActivity extends Activity implements MyFragment.SendData{
       private Button btn;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       btn = (Button) findViewById(R.id.send);
       btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        MyFragment myFragment = new MyFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
        beginTransaction.add(R.id.layout, myFragment);
        beginTransaction.commit();
        }
       });
       }
       //實(shí)現(xiàn)SendData接口,接收數(shù)據(jù)
       @Override
       public void sendMsg(String str) {
       Toast.makeText(this, "成功接收\"" + str + "\"", Toast.LENGTH_SHORT).show();
       }
      }
      

      以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持創(chuàng)新互聯(lián)!


      本文標(biāo)題:Android中Fragment的加載方式與數(shù)據(jù)通信詳解
      轉(zhuǎn)載源于:http://www.ef60e0e.cn/article/ipegej.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        泉州市| 广德县| 广东省| 张北县| 峡江县| 青龙| 建平县| 清流县| 龙州县| 灯塔市| 揭西县| 昌吉市| 贵南县| 那曲县| 吉安市| 思茅市| 射阳县| 阜阳市| 商丘市| 江都市| 富源县| 河南省| 洛浦县| 荆门市| 运城市| 嘉善县| 商河县| 兴义市| 富顺县| 乐平市| 陈巴尔虎旗| 石棉县| 普陀区| 凌源市| 苍山县| 吴堡县| 鸡西市| 翁牛特旗| 张家界市| 礼泉县| 治县。|