{"id":962,"date":"2017-03-18T15:19:53","date_gmt":"2017-03-18T07:19:53","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=962"},"modified":"2017-03-18T15:19:53","modified_gmt":"2017-03-18T07:19:53","slug":"android%e5%9f%ba%e7%a1%80%e5%bc%80%e5%8f%91%e4%b9%8bfragment%e7%9a%84%e7%ae%80%e5%8d%95%e4%bd%bf%e7%94%a8%e5%8a%a8%e6%80%81%e5%8a%a0%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/android\/2017\/03\/18\/151953962.html","title":{"rendered":"Android\u57fa\u7840\u5f00\u53d1\u4e4bFragment\u7684\u7b80\u5355\u4f7f\u7528(\b\u52a8\u6001\u52a0\u8f7d)"},"content":{"rendered":"<p>1\u3001\u53c2\u7167<a href=\"http:\/\/www.yusian.com\/blog\/android\/2017\/03\/18\/144836.html\" target=\"_blank\">Android\u57fa\u7840\u5f00\u53d1\u4e4bFragment\u7684\u7b80\u5355\u4f7f\u7528(\u9759\u6001\u52a0\u8f7d)<\/a>\u6dfb\u52a0\u4e24\u4e2aFragment\u7684\u7c7b\u53ca\u5e03\u5c40\u6587\u4ef6\uff1b<br \/>\n2\u3001\u5728Activity\u7684\u5e03\u5c40\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e24\u4e2a\u6309\u94ae\u7528\u6765\u8fdb\u884cFragment\u7684\u5207\u6362\uff1b<br \/>\n3\u3001\u5728Activity\u7684\u5e03\u5c40\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2aFrameLayout\u5e03\u5c40\u7528\u4e8eFragment\u7684\u663e\u793a\uff1b<\/p>\n<pre line=\"1\" lang=\"java\"><?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout\n    xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\"\n    android:weightSum=\"2\"\n    tools:context=\"com.yusian.fragment.MainActivity\">\n\n    <Button\n        android:id=\"@+id\/btn_frag_a\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"A\"\/>\n\n    <Button\n        android:id=\"@+id\/btn_frag_b\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"B\"\/>\n\n    <FrameLayout\n        android:id=\"@+id\/fl_fragment\"\n        android:layout_weight=\"1\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\">\n\n    <\/FrameLayout>\n<\/LinearLayout><\/pre>\n<p>4\u3001Fragment\u7684\u5207\u6362\u6d89\u53ca\u5230Fragment\u7684\u4e24\u4e2a\u7c7b\uff1aFragmentManager\u4e0eFragmentTransaction;<!--more--><br \/>\n5\u3001\u5207\u6362Fragment\u4e09\u6b65\u66f2\uff1abeginTransaction()&#8212;>replace()&#8212;>commit();<\/p>\n<pre line=\"1\" lang=\"java\">package com.yusian.fragment;\n\nimport android.app.FragmentManager;\nimport android.app.FragmentTransaction;\nimport android.support.v7.app.AppCompatActivity;\nimport android.os.Bundle;\nimport android.view.View;\nimport android.widget.Button;\n\npublic class MainActivity extends AppCompatActivity implements View.OnClickListener{\n    Button fragmentA, fragmentB;\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        fragmentA = (Button)findViewById(R.id.btn_frag_a);\n        fragmentB = (Button)findViewById(R.id.btn_frag_b);\n        fragmentA.setOnClickListener(this);\n        fragmentB.setOnClickListener(this);\n    }\n\n    @Override\n    public void onClick(View v) {\n        FragmentManager fragMgr = getFragmentManager();\n        FragmentTransaction transaction = fragMgr.beginTransaction();\n        if (v == fragmentA){\n            System.out.println(\"\u70b9\u51fb\u4e86A\u6309\u94ae\");\n            transaction.replace(R.id.fl_fragment, new AFragment());\n        }else{\n            System.out.println(\"\u70b9\u51fb\u4e86B\u6309\u94ae\");\n            transaction.replace(R.id.fl_fragment, new BFragment());\n        }\n        transaction.commit();\n    }\n}<\/pre>\n<p>6\u3001\u6548\u679c\u56fe<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2017\/03\/device-2017-03-18-151636.png\" alt=\"\" width=\"300\" height=\"533\" class=\"alignnone size-full wp-image-963\" \/> <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2017\/03\/device-2017-03-18-151623.png\" alt=\"\" width=\"300\" height=\"533\" class=\"alignnone size-medium wp-image-964\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u53c2\u7167Android\u57fa\u7840\u5f00\u53d1\u4e4bFragment\u7684\u7b80\u5355\u4f7f\u7528(\u9759\u6001\u52a0\u8f7d)\u6dfb\u52a0\u4e24\u4e2aFragment\u7684\u7c7b\u53ca\u5e03\u5c40\u6587\u4ef6\uff1b 2\u3001\u5728Activity\u7684\u5e03\u5c40\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e24\u4e2a\u6309\u94ae\u7528\u6765\u8fdb\u884cFragment\u7684\u5207\u6362\uff1b 3\u3001\u5728Activity\u7684\u5e03\u5c40\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2aFrameLayout\u5e03\u5c40\u7528\u4e8eFragment\u7684\u663e\u793a\uff1b 4\u3001Fragment\u7684\u5207\u6362\u6d89\u53ca\u5230Fragment\u7684\u4e24\u4e2a\u7c7b\uff1aFragmentManager\u4e0eFragmentTransaction;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[33],"class_list":["post-962","post","type-post","status-publish","format-standard","hentry","category-android","tag-33"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=962"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/962\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}