{"id":1578,"date":"2022-12-08T10:00:00","date_gmt":"2022-12-08T02:00:00","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1578"},"modified":"2023-02-03T11:32:21","modified_gmt":"2023-02-03T03:32:21","slug":"%e3%80%90jetpack%e3%80%91android-viewbinding-%e8%a6%96%e5%9c%96%e7%b6%81%e5%ae%9a-%e7%af%84%e4%be%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/%e3%80%90jetpack%e3%80%91android-viewbinding-%e8%a6%96%e5%9c%96%e7%b6%81%e5%ae%9a-%e7%af%84%e4%be%8b\/","title":{"rendered":"\ud83d\udd17\u3010Jetpack\u3011Android ViewBinding \u8996\u5716\u7d81\u5b9a \u7bc4\u4f8b\ud83c\udfaf"},"content":{"rendered":"<h1>\ud83d\udd17\u3010Jetpack\u3011Android ViewBinding \u8996\u5716\u7d81\u5b9a \u7bc4\u4f8b\ud83c\udfaf<\/h1>\n<h4>Android ViewBinding \u662f Google \u63a8\u51fa\u7684\u7528\u65bc\u4fbf\u5229\u958b\u767c Android \u61c9\u7528\u7684\u4e00\u7a2e\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u6709\u6548\u7c21\u5316\u8a2a\u554f Layout \u7684\u7e41\u7463\u64cd\u4f5c\uff0c\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u8868\u73fe\u529b\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/h4>\n<h4>ViewBinding \u901a\u904e\u5411\u958b\u767c\u8005\u63d0\u4f9b\u6240\u6709 UI \u5143\u7d20\u7684\u5be6\u4f8b\u4f86\u6e1b\u5c11\u4ee3\u78bc\u7684\u91cd\u8907\u91cf\uff0c\u9032\u800c\u6e1b\u5c11\u8207\u754c\u9762\u9023\u63a5\u7684\u6642\u9593\u548c\u7cbe\u529b\u6210\u672c\uff0c\u540c\u6642\u964d\u4f4e\u5d29\u6f70\u7684\u6982\u7387\u548c\u51fa\u932f\u7684\u53ef\u80fd\u6027\u3002<\/h4>\n<h4>\u672c\u6587\u4ecb\u7d39\u4e86 Android ViewBinding \u7684\u529f\u80fd\u3001\u4f18\u70b9\u4ee5\u53ca\u5728\u6846\u67b6\u4e2d\u7684\u61c9\u7528\uff0c\u5256\u6790\u4e86 ViewBinding \u6280\u8853\u5c0d Android \u958b\u767c\u7684\u5f71\u97ff\u548c\u5e36\u4f86\u7684\u512a\u52e2\u3002<\/h4>\n<hr \/>\n<h4>\u6587\u7ae0\u76ee\u9304<\/h4>\n<ol>\n<li><a href=\"#a\">ViewBinding &#038; Fragment KTX \u5c0e\u5165<\/a><\/li>\n<li><a href=\"#b\">ViewBinding Layout<\/a><\/li>\n<li><a href=\"#c\">ViewBinding MainActivity<\/a><\/li>\n<li><a href=\"#d\">ViewBinding InflateFragment<\/a><\/li>\n<li><a href=\"#e\">ViewBinding BindFragment<\/a><\/li>\n<li><a href=\"#f\">Developer Documents ViewBinding<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h4>1.ViewBinding &amp; Fragment KTX \u5c0e\u5165<\/h4>\n<h5>build.gradle<\/h5>\n<pre><code class=\"language-groovy line-numbers\">android {\n\n    buildFeatures {\n        viewBinding = true\n    }\n}\n\ndependencies {\n    def fragment_version = \"1.5.5\"\n    implementation \"androidx.fragment:fragment-ktx:$fragment_version\"\n}\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h4>2.ViewBinding Layout<\/h4>\n<h5>activity_main.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    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\".MainActivity\"&gt;\n\n    &lt;androidx.constraintlayout.widget.ConstraintLayout\n        android:id=\"@+id\/bind\"\n        android:layout_width=\"0dp\"\n        android:layout_height=\"0dp\"\n        app:layout_constraintBottom_toTopOf=\"@+id\/guideline3\"\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\n        android:id=\"@+id\/inflate\"\n        android:layout_width=\"0dp\"\n        android:layout_height=\"0dp\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"@+id\/guideline2\" \/&gt;\n\n    &lt;TextView\n        android:id=\"@+id\/name\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:textSize=\"50sp\"\n        app:layout_constraintBottom_toTopOf=\"@+id\/guideline2\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"@+id\/guideline3\" \/&gt;\n\n    &lt;androidx.constraintlayout.widget.Guideline\n        android:id=\"@+id\/guideline2\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:orientation=\"horizontal\"\n        app:layout_constraintGuide_percent=\"0.66\" \/&gt;\n\n    &lt;androidx.constraintlayout.widget.Guideline\n        android:id=\"@+id\/guideline3\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:orientation=\"horizontal\"\n        app:layout_constraintGuide_percent=\"0.33\" \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>3.ViewBinding MainActivity<\/h4>\n<h5>MainActivity.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class MainActivity : AppCompatActivity() {\n\n    private lateinit var binding: ActivityMainBinding\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n\n        binding = ActivityMainBinding.inflate(layoutInflater)\n        binding.name.text = \"Activity\"\n        setContentView(binding.root)\n    }\n\n    override fun onStart() {\n        super.onStart()\n\n        supportFragmentManager.commit {\n            add&lt;BindFragment&gt;(R.id.bind)\n            add&lt;InflateFragment&gt;(R.id.inflate)\n            setReorderingAllowed(true)\n            addToBackStack(\"name\")\n        }\n    }\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h4>4.ViewBinding InflateFragment<\/h4>\n<h5>InflateFragment.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class InflateFragment : Fragment() {\n\n    private var fragmentInflateBinding: FragmentInflateBinding? = null\n\n    override fun onCreateView(\n        inflater: LayoutInflater, container: ViewGroup?,\n        savedInstanceState: Bundle?,\n    ): View {\n        val binding = FragmentInflateBinding.inflate(inflater, container, false)\n        fragmentInflateBinding = binding\n        binding.name.text = \"InflateFragment\"\n        return binding.root\n    }\n\n    override fun onDestroyView() {\n        fragmentInflateBinding = null\n        super.onDestroyView()\n    }\n}\n<\/code><\/pre>\n<p><a id=\"e\"><\/a><\/p>\n<h4>5.ViewBinding BindFragment<\/h4>\n<h5>BindFragment.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class BindFragment : Fragment(R.layout.fragment_bind) {\n\n    private var fragmentBindBinding: FragmentBindBinding? = null\n\n    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {\n        super.onViewCreated(view, savedInstanceState)\n        val binding = FragmentBindBinding.bind(view)\n        fragmentBindBinding = binding\n        binding.name.text = \"BindFragment\"\n    }\n\n    override fun onDestroyView() {\n        fragmentBindBinding = null\n        super.onDestroyView()\n    }\n}\n<\/code><\/pre>\n<p><a id=\"f\"><\/a><\/p>\n<h4>6.Developer Documents ViewBinding<\/h4>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developer.android.com\/topic\/libraries\/view-binding\" title=\"Open in Documents ViewBinding\" target=\"_blank\" rel=\"noopener\">Open in Documents ViewBinding<\/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>\ud83d\udd17\u3010Jetpack\u3011Android ViewBinding \u8996\u5716\u7d81\u5b9a \u7bc4\u4f8b\ud83c\udfaf Android ViewBind &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":[106],"tags":[13,107,216],"class_list":["post-1578","post","type-post","status-publish","format-standard","hentry","category-jetpack","tag-android","tag-jetpack","tag-viewbinding"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1578","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=1578"}],"version-history":[{"count":6,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1578\/revisions"}],"predecessor-version":[{"id":1842,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1578\/revisions\/1842"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}