{"id":1809,"date":"2023-01-13T10:21:10","date_gmt":"2023-01-13T02:21:10","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1809"},"modified":"2023-01-13T10:21:10","modified_gmt":"2023-01-13T02:21:10","slug":"%e3%80%90android%e3%80%91fragmnet-%e7%89%87%e6%ae%b5-%e7%af%84%e4%be%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/%e3%80%90android%e3%80%91fragmnet-%e7%89%87%e6%ae%b5-%e7%af%84%e4%be%8b\/","title":{"rendered":"\u3010Android\u3011Fragmnet \u7247\u6bb5 \u7bc4\u4f8b"},"content":{"rendered":"<h1>\u3010Android\u3011Fragmnet \u7247\u6bb5 \u7bc4\u4f8b<\/h1>\n<h4>Android Fragment \u662f\u4e00\u500b\u53ef\u4ee5\u88ab\u653e\u7f6e\u5728 Android Activity \u4e2d\u7684 UI \u90e8\u4ef6\u3002\u5b83\u53ef\u4ee5\u88ab\u7528\u4f86\u7d44\u6210\u591a\u500b\u9801\u9762\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u4e26\u4e14\u5141\u8a31\u5728\u4e0d\u540c\u7684\u87a2\u5e55\u5c3a\u5bf8\u6216\u8a2d\u5099\u4e0a\u91cd\u7528 UI\u3002Fragment \u53ef\u4ee5\u88ab\u52a0\u5165\u6216\u79fb\u9664\uff0c\u4e26\u4e14\u53ef\u4ee5\u5728\u4e0d\u540c\u7684 Activity \u4e4b\u9593\u5171\u7528\u3002<\/h4>\n<h4>\u5b83\u53ef\u4ee5\u642d\u914d Android \u7684 FragmentManager \u548c FragmentTransaction \u985e\u5225\u4f86\u7ba1\u7406\u548c\u64cd\u4f5c\u3002<\/h4>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developer.android.com\/static\/images\/guide\/fragments\/fragment-screen-sizes.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/developer.android.com\/static\/images\/guide\/fragments\/fragment-screen-sizes.png\" alt=\"\" \/><\/a><\/p>\n<hr \/>\n<h4>\u6587\u7ae0\u76ee\u9304<\/h4>\n<ol>\n<li><a href=\"#a\">Fragment \u5275\u5efa<\/a><\/li>\n<li><a href=\"#b\">Fragment \u53c3\u6578\u50b3\u905e<\/a><\/li>\n<li><a href=\"#c\">Fragment \u6dfb\u52a0\u4f9d\u8cf4<\/a><\/li>\n<li><a href=\"#d\">Developer Documents Fragment<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h4>1.Fragment \u5275\u5efa<\/h4>\n<h5>fragment_first.xml<\/h5>\n<pre data-language=XML><code class=\"language-markup line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"&gt;\n\n    &lt;androidx.appcompat.widget.AppCompatTextView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"First\"\n        android:textSize=\"50sp\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;\n<\/code><\/pre>\n<h5>FirstFragment.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class FirstFragment : Fragment(R.layout.fragment_first) {\n\n}\n<\/code><\/pre>\n<h5>activity_main.xml<\/h5>\n<pre data-language=XML><code class=\"language-markup line-numbers\">&lt;androidx.constraintlayout.widget.ConstraintLayout\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"&gt;\n\n    &lt;androidx.fragment.app.FragmentContainerView\n        android:id=\"@+id\/fragment_container_view\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\" \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;\n<\/code><\/pre>\n<h5>MainActivity.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class MainActivity : AppCompatActivity(R.layout.activity_main) {\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n\n        if (savedInstanceState == null) {\n            supportFragmentManager.commit {\n                setReorderingAllowed(true)\n                add&lt;FirstFragment&gt;(R.id.fragment_container_view)\n            }\n        }\n    }\n\n}\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h4>2.Fragment \u53c3\u6578\u50b3\u905e<\/h4>\n<h5>MainActivity.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class MainActivity : AppCompatActivity(R.layout.activity_main) {\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n\n        if (savedInstanceState == null) {\n            val bundle = bundleOf(\"number\" to 5)\n            supportFragmentManager.commit {\n                setReorderingAllowed(true)\n                add&lt;FirstFragment&gt;(R.id.fragment_container_view, args = bundle)\n            }\n        }\n    }\n\n}\n<\/code><\/pre>\n<h5>FirstFragment.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class FirstFragment : Fragment(R.layout.fragment_first) {\n\n    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {\n        val number = requireArguments().getInt(\"number\")\n        Log.e(\"FirstFragment\", number.toString())\n    }\n\n}\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>3.Fragment \u6dfb\u52a0\u4f9d\u8cf4<\/h4>\n<h5>PersonData.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">data class PersonData(val name: String, val age: Int)\n<\/code><\/pre>\n<h5>FirstFragment.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class FirstFragment(private val personData: PersonData) : Fragment(R.layout.fragment_first) {\n\n    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {\n        Log.e(\"FirstFragment\", personData.toString())\n    }\n\n}\n<\/code><\/pre>\n<h5>FirstFragmentFactory.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class FirstFragmentFactory(private val personData: PersonData) : FragmentFactory() {\n\n    override fun instantiate(classLoader: ClassLoader, className: String): Fragment =\n        when (loadFragmentClass(classLoader, className)) {\n            FirstFragment::class.java -&gt; FirstFragment(personData)\n            else -&gt; super.instantiate(classLoader, className)\n        }\n\n}\n<\/code><\/pre>\n<h5>MainActivity.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class MainActivity : AppCompatActivity(R.layout.activity_main) {\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n\n        if (savedInstanceState == null) {\n            supportFragmentManager.fragmentFactory = FirstFragmentFactory(PersonData(\"wade\", 26))\n\n            supportFragmentManager.commit {\n                add&lt;FirstFragment&gt;(R.id.fragment_container_view)\n                setReorderingAllowed(true)\n            }\n        }\n    }\n\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h4>4.Developer Documents Fragment<\/h4>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developer.android.com\/guide\/fragments\" title=\"Open in Documents Fragment\" target=\"_blank\" rel=\"noopener\">Open in Documents Fragment<\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u3010Android\u3011Fragmnet \u7247\u6bb5 \u7bc4\u4f8b Android Fragment \u662f\u4e00\u500b\u53ef\u4ee5\u88ab\u653e\u7f6e\u5728 Andr &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,229,15],"class_list":["post-1809","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-fragment","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1809"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1809\/revisions"}],"predecessor-version":[{"id":1811,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1809\/revisions\/1811"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}