{"id":1642,"date":"2023-03-25T21:42:38","date_gmt":"2023-03-25T13:42:38","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1642"},"modified":"2023-04-23T21:59:10","modified_gmt":"2023-04-23T13:59:10","slug":"getting-started-with-android-viewbinding","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/getting-started-with-android-viewbinding\/","title":{"rendered":"Android ViewBinding\u5165\u95e8"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u968f\u7740Android Studio 3.6\u7684\u6b63\u5f0f\u53d1\u5e03\uff0cGradle\u63d2\u4ef6\u5347\u7ea7\u52303.6.0\uff0cButterKnife\u62a5\u9519\uff0c\u65e5\u5fd7\u5982\u4e0b\uff1a<\/p>\n<pre><code>Attempt to use @BindView for an already bound ID 0 on &#039;mTvText&#039;.<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u56de\u9000Gradle\u7248\u672c\u540e\u56de\u5f52\u6b63\u5e38\uff0c\u4e8b\u5b9e\u4e0a\uff0cButterKnife\u4f5c\u8005\u5df2\u4e0d\u63a8\u8350\u4f7f\u7528ButterKnife\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662fViewBinding<\/p>\n<h2>\u521d\u8bc6ViewBinding<\/h2>\n<p>ViewBinding\u6587\u6863\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/developer.android.google.cn\/topic\/libraries\/view-binding\">https:\/\/developer.android.google.cn\/topic\/libraries\/view-binding<\/a><\/p>\n<p>ViewBinding\u548cButterKnife\u4e00\u6837\u90fd\u662f\u4e3a\u4e86\u7701\u53bb<code>findViewById()<\/code>\u8fd9\u6837\u7684\u91cd\u590d\u4ee3\u7801\u3002\u5176\u5b9e\u57282019\u8c37\u6b4c\u5f00\u53d1\u8005\u5cf0\u4f1a\u4e0a\u5bf9ViewBinding\u5c31\u5df2\u7ecf\u6709\u6240\u8033\u95fb\uff0clayout\u4e2d\u66f4\u65b0\u63a7\u4ef6ID\u540e\u7acb\u523b\u53ef\u4ee5\u5728Activity\u4e2d\u5f15\u7528\u5230\uff0c\u8fd9\u7edd\u5bf9\u6bd4ButterKnife\u9700\u8981\u7f16\u8bd1\u3001\u9700\u8981\u533a\u5206R\u548cR2\u8981\u8212\u670d\u7684\u591a\u3002<\/p>\n<h2>\u4f7f\u7528ViewBinding<\/h2>\n<h3>\u73af\u5883\u8981\u6c42<\/h3>\n<ul>\n<li>Android Studio\u7248\u672c3.6\u53ca\u4ee5\u4e0a<\/li>\n<li>Gradle \u63d2\u4ef6\u7248\u672c3.6.0\u53ca\u4ee5\u4e0a<\/li>\n<\/ul>\n<h2>\u5f00\u542fViewBinding\u529f\u80fd<\/h2>\n<p>ViewBinding\u652f\u6301\u6309\u6a21\u5757\u542f\u7528\uff0c\u5728\u6a21\u5757\u7684<code>build.gradle<\/code>\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">android {\n    ...\n    viewBinding {\n        enabled = true\n    }\n}    <\/code><\/pre>\n<h3>Activity\u4e2dViewBinding\u7684\u4f7f\u7528<\/h3>\n<pre><code class=\"language-java\">\/\/\u4e4b\u524d\u8bbe\u7f6e\u89c6\u56fe\u7684\u65b9\u6cd5\nsetContentView(R.layout.activity_main);\n\n\/\/\u4f7f\u7528ViewBinding\u540e\u7684\u65b9\u6cd5\nmBinding = ActivityMainBinding.inflate(getLayoutInflater());\nsetContentView(mBinding.getRoot());<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5f53\u4f7f\u7528\u4e86ViewBinding\u540e\uff0c\u9488\u5bf9<code>activity_main.xml<\/code>\u6587\u4ef6\uff0c\u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a<code>ActivityMainBinding.java<\/code>\u6587\u4ef6\uff08\u8be5\u6587\u4ef6\u5728<code>build\/generated\/data_binding_base_class_source_out\/xxx\u2026<\/code>\u76ee\u5f55\u4e0b\uff09\uff0c\u4e5f\u5c31\u662f\u5e03\u5c40\u6587\u4ef6\u7684\u9a7c\u5cf0\u547d\u540d\u6cd5\u52a0\u4e0a\u4e00\u4e2aBinding\u540e\u7f00\uff0c\u7136\u540e\u5728Activity\u4e2d\u76f4\u63a5\u4f7f\u7528\u5c31\u53ef\u4ee5\u3002<\/p>\n<h4>\u5e03\u5c40\u4e2d\u76f4\u63a5\u7684\u63a7\u4ef6<\/h4>\n<p>\u5f53\u6211\u4eec\u5728\u5e03\u5c40\u4e2d\u6dfb\u52a0\u4e00\u4e2aid\u4e3a<code>tv_text<\/code>\u7684TextView\u540e\uff0c\u76f4\u63a5\u5728Activity\u4e2d\u4f7f\u7528<code>mBinding.tvText<\/code>\u5373\u53ef\u62ff\u5230\u8be5\u63a7\u4ef6\u3002\u5982\u4e0b\u6240\u793a\uff0c\u53ef\u4ee5\u770b\u5230\u4e5f\u662f\u4ee5\u63a7\u4ef6ID\u7684\u9a7c\u5cf0\u547d\u540d\u6cd5\u6765\u83b7\u53d6\u7684:<\/p>\n<pre><code class=\"language-java\">mBinding.tvText.setText(&quot;ViewBinding\u6d4b\u8bd5&quot;);<\/code><\/pre>\n<h4>\u5e03\u5c40\u4e2d\u4f7f\u7528include<\/h4>\n<p>\u4f8b\u5982\u6211\u4eec\u6709\u4e2a<code>layout_comment.xml<\/code>\u7684\u5e03\u5c40\uff0c\u5e03\u5c40\u4e2d\u6709id\u4e3a<code>tv_include<\/code>\u7684TextView\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;match_parent&quot;&gt;\n\n    &lt;TextView\n        android:id=&quot;@+id\/tv_include&quot;\n        android:text=&quot;include\u6d4b\u8bd5&quot;\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;match_parent&quot; \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;<\/code><\/pre>\n<p>\u7136\u540e\u5728<code>activity_main.xml<\/code>\u6587\u4ef6\u4e2dinclude\u8be5\u5e03\u5c40\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    xmlns:app=&quot;http:\/\/schemas.android.com\/apk\/res-auto&quot;\n    xmlns:tools=&quot;http:\/\/schemas.android.com\/tools&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;match_parent&quot;\n    tools:context=&quot;.MainActivity&quot;&gt;\n\n    &lt;TextView\n        android:id=&quot;@+id\/tv_text&quot;\n        android:layout_width=&quot;wrap_content&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:text=&quot;Hello World!&quot;\n        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;\n        app:layout_constraintLeft_toLeftOf=&quot;parent&quot;\n        app:layout_constraintRight_toRightOf=&quot;parent&quot;\n        app:layout_constraintTop_toTopOf=&quot;parent&quot; \/&gt;\n\n    &lt;include\n        android:id=&quot;@+id\/layout_include&quot;\n        layout=&quot;@layout\/layout_comment&quot; \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;<\/code><\/pre>\n<p>\u90a3\u4e48\u6b64\u65f6\u6211\u4eec\u5982\u4f55\u4f7f\u7528\u5230<code>layout_comment.xml<\/code>\u5e03\u5c40\u4e2d\u7684TextView\u63a7\u4ef6\u5462\uff0c\u9996\u5148include\u6807\u7b7e\u9700\u8981\u58f0\u660eid\uff0c\u4f8b\u5982<code>layout_include<\/code>\uff0c\u7136\u540eActivity\u4e2d\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">mBinding.layoutInclude.tvInclude.setText(&quot;ViewBinding include\u6d4b\u8bd5&quot;);<\/code><\/pre>\n<p>\u662f\u4e0d\u662f\u5f88\u795e\u5947\uff0c\u662f\u4e0d\u662f\u5f88\u7b80\u5355<\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u5f53\u7ed9<code>layout_comment.xml<\/code>\u7684\u6839\u5e03\u5c40\u518d\u6dfb\u52a0id\uff08\u6bd4\u5982\u6dfb\u52a0\u4e86<code>layout_xxx<\/code>\u7684ID\uff09\u7684\u65f6\u5019\uff0c\u6b64\u65f6\u4f1a\u62a5\u9519\uff1a<\/p>\n<\/blockquote>\n<pre><code>java.lang.NullPointerException: Missing required view with ID: layout_xxx<\/code><\/pre>\n<h4>\u5e03\u5c40\u4e2d\u4f7f\u7528include\u548cmerge<\/h4>\n<p>\u6211\u4eec\u5c06\u4e0a\u6587\u7684<code>layout_comment.xml<\/code>\u7a0d\u4f5c\u4fee\u6539\uff0c\u6839\u5e03\u5c40\u4f7f\u7528<code>merge<\/code>\u6807\u7b7e\uff0c\u5176\u4ed6\u4e0d\u505a\u4fee\u6539:<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;merge xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;wrap_content&quot;&gt;\n\n    &lt;TextView\n        android:id=&quot;@+id\/tv_include&quot;\n        android:text=&quot;merge\u6d4b\u8bd5&quot;\n        android:gravity=&quot;end&quot;\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;match_parent&quot; \/&gt;\n\n&lt;\/merge&gt;<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    xmlns:app=&quot;http:\/\/schemas.android.com\/apk\/res-auto&quot;\n    xmlns:tools=&quot;http:\/\/schemas.android.com\/tools&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;match_parent&quot;\n    tools:context=&quot;.MainActivity&quot;&gt;\n\n    &lt;include\n        android:id=&quot;@+id\/layout_include&quot;\n        layout=&quot;@layout\/layout_comment&quot; \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;<\/code><\/pre>\n<p><code>activity_main.xml<\/code>\u6587\u4ef6\u4e2d\u4f7f\u7528include\u6dfb\u52a0\u8be5\u5e03\u5c40\u540e\uff0c\u5728java\u4ee3\u7801\u4e2d\u4f9d\u65e7\u662f\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u7684\uff1a<\/p>\n<pre><code class=\"language-java\">mBinding.layoutInclude.tvInclude.setText(&quot;ViewBinding merge\u6d4b\u8bd5&quot;);<\/code><\/pre>\n<p>\u4f46\u662f\u4f46\u662f\uff01\uff01\uff01\u8fd0\u884c\u5c31\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre><code>java.lang.NullPointerException: Missing required view with ID: layoutInclude<\/code><\/pre>\n<p>\u8981\u662f\u628ainclude\u6807\u7b7e\u7684id\u53bb\u6389\u7684\u8bdd\uff0c\u8fd9\u65f6mBinding\u4e2d\u4e5f\u662f\u627e\u4e0d\u5230<code>tvInclude<\/code>\u8fd9\u4e2a\u63a7\u4ef6\u5440\uff0c\u600e\u4e48\u529e\uff1f\uff1f<br \/>\n\u4e4b\u524d\u662f\u4e0d\u662f\u8bf4\u8fc7\uff0c\u6bcf\u4e2alayout\u6587\u4ef6\u90fd\u4f1a\u5bf9\u5e94\u4e00\u4e2aBinding\u6587\u4ef6\uff0c\u90a3\u4e48<code>layout_comment.xml<\/code>\uff0c\u80af\u5b9a\u4e5f\u6709\u4e00\u4e2a<code>LayoutCommentBinding.java<\/code>\u6587\u4ef6\uff0c\u6211\u4eec\u53bb\u770b\u4e0b\u8fd9\u4e2a\u6587\u4ef6\u7684\u6e90\u4ee3\u7801\uff0c\u91cc\u9762\u6709\u4e2a\u53ef\u7591\u7684\u65b9\u6cd5\uff0c\u5373<code>bind()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">@NonNull\npublic static LayoutCommentBinding bind(@NonNull View rootView) {\n    \/\/ The body of this method is generated in a way you would not otherwise write.\n    \/\/ This is done to optimize the compiled bytecode for size and performance.\n    String missingId;\n    missingId: {\n        TextView tvInclude = rootView.findViewById(R.id.tv_include);\n        if (tvInclude == null) {\n            missingId = &quot;tvInclude&quot;;\n            break missingId;\n        }\n        return new LayoutCommentBinding(rootView, tvInclude);\n    }\n    throw new NullPointerException(&quot;Missing required view with ID: &quot;.concat(missingId));\n}<\/code><\/pre>\n<p>\u6240\u4ee5\u5bf9\u4e8e\u542b\u6709merge\u6807\u7b7e\u7684\u5e03\u5c40\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>bind()<\/code>\u65b9\u6cd5\u6765\u7ed1\u5b9a\u5230\u6839\u5e03\u5c40\u4e0a\uff0c\u5728\u8fd9\u91cc\uff0c\u6839\u5e03\u5c40\u5c31\u662f<code>mBinding.getRoot()<\/code>\u3002\u6240\u4ee5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u8fd9\u4e48\u5199\u4e0d\u53ef\u4ee5\n\/\/mBinding.layoutInclude.tvInclude.setText(&quot;ViewBinding merge\u6d4b\u8bd5&quot;);\n\nLayoutCommentBinding commentBinding = LayoutCommentBinding.bind(mBinding.getRoot());\ncommentBinding.tvInclude.setText(&quot;ViewBinding merge\u6d4b\u8bd5&quot;);<\/code><\/pre>\n<p>\u540c\u65f6\u9700\u8981\u6ce8\u610f\uff1a<code>include<\/code>\u6807\u7b7e\u4e0d\u53ef\u4ee5\u6709<code>id<\/code><\/p>\n<h3>Fragment\u4e2d\u4f7f\u7528ViewBinding<\/h3>\n<p>\u5728Fragment\u7684<code>onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)<\/code>\u65b9\u6cd5\u4e2d\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u539f\u6765\u7684\u5199\u6cd5\nreturn inflater.inflate(R.layout.fragment_blank, container, false);\n\n\/\/\u4f7f\u7528ViewBinding\u7684\u5199\u6cd5\nmBinding = FragmentBlankBinding.inflate(inflater);\nreturn mBinding.getRoot();<\/code><\/pre>\n<p>\u62ff\u5230FragmentBlankBinding\u7684\u5bf9\u8c61\u540e\uff0c\u66f4\u65b0\u6570\u636e\u7684\u90fd\u548c\u4e4b\u524d\u4e00\u6837\u3002<\/p>\n<h3>\u81ea\u5b9a\u4e49Dialog\u4e2d\u4f7f\u7528ViewBinding<\/h3>\n<p>Dialog\u4e2d\u4f7f\u7528\u548cActivity\u4ee5\u53caFragment\u4e00\u6837\uff0c\u76f4\u63a5\u4f7f\u7528\u5355\u53c2\u6570\u7684<code>inflate()<\/code>\u65b9\u6cd5\u5373\u53ef\uff0c\u4f2a\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public class MyDialog extends Dialog {\n\n    protected View mView;\n    protected DialogBottomBinding mBinding;\n\n    public MyDialog(@NonNull Context context, @StyleRes int themeResId) {\n        super(context, themeResId);\n\n        \/\/\u539f\u6765\u7684\u5199\u6cd5\n        mView = View.inflate(getContext(), getLayoutId(), null);\n\n        \/\/\u4f7f\u7528ViewBinding\u7684\u5199\u6cd5\n        mBinding = DialogBottomBinding.inflate(getLayoutInflater());\n        mView = mBinding.getRoot();\n\n        setContentView(mView);\n    }\n}<\/code><\/pre>\n<h3>\u81ea\u5b9a\u4e49View\u4e2d\u4f7f\u7528ViewBinding<\/h3>\n<p>\u5728\u91cd\u6784\u5de5\u7a0b\u7684\u65f6\u5019\u53d1\u73b0\u81ea\u5b9a\u4e49\u89c6\u56fe\u4e2d\u5176\u5b9e\u6709\u5f88\u591a\u95ee\u9898\uff0c\u8fd9\u91cc\u628a\u8fd9\u4e24\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u603b\u7ed3\u4e0b\uff1a<\/p>\n<h4>\u4f7f\u7528\u7684layout\u6587\u4ef6\u4e0d\u5305\u542bmerge<\/h4>\n<p>\u8fd9\u91cc\u76f4\u63a5\u8d34\u51fa\u6765\u4ee3\u7801\u5427\uff0c\u5c31\u662f\u81ea\u5b9a\u4e49\u4e86\u4e00\u4e2aLinearLayout\u7136\u540e\u5f80\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5e03\u5c40\uff0c\u8be5\u5e03\u5c40\u662f<code>view_my_layout.xml<\/code>\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;match_parent&quot;&gt;\n\n    &lt;TextView\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;match_parent&quot;\n        android:gravity=&quot;center&quot;\n        android:text=&quot;\u8fd9\u662f\u81ea\u5b9a\u4e49\u5e03\u5c40&quot;\n        android:textSize=&quot;50sp&quot; \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;<\/code><\/pre>\n<p>\u4f1a\u751f\u6210\u4e00\u4e2a\u5bf9\u5e94\u7684<code>ViewMyLayoutBinding.java<\/code>\u6587\u4ef6\uff0c\u770b\u4e0b\u6587<code>MyLinearLayout<\/code>\u4ee3\u7801\uff1a<br \/>\n<code>init1\u30012\u30013\u30014<\/code>\u662f\u4f7f\u7528inflate\u6765\u5bfc\u5165layout\u5e03\u5c40\u7684\u5199\u6cd5\uff0c\u5168\u90e8\u53ef\u4ee5\u6b63\u5e38\u663e\u793a\u81ea\u5b9a\u4e49\u7684\u5e03\u5c40\u3002<br \/>\n<code>init10\u300111\u300112<\/code>\u662f\u4f7f\u7528ViewBinding\u7684\u5199\u6cd5\uff0c10\u65e0\u6cd5\u6b63\u5e38\u663e\u793a\u89c6\u56fe\uff0c11\u548c12\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u5199\u6cd5\uff0c\u9053\u7406\u4e00\u6837\u3002<\/p>\n<pre><code class=\"language-java\">public class MyLinearLayout extends LinearLayout {\n    public MyLinearLayout(Context context) {\n        this(context, null);\n    }\n\n    public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {\n        this(context, attrs, 0);\n    }\n\n    public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n\n\/\/        init1();\n\/\/        init2();\n\/\/        init3();\n        init4();\n    }\n\n    private void init1() {\n        inflate(getContext(), R.layout.view_my_layout, this);\n    }\n\n    private void init2() {\n        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_my_layout, this);\n    }\n\n    \/\/\u548cinit2()\u65b9\u6cd5\u76f8\u7b49\n    private void init3() {\n        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_my_layout, this, true);\n    }\n\n    private void init4() {\n        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_my_layout, this, false);\n        addView(view);\n    }\n\n    \/\/\u89c6\u56fe\u5f02\u5e38\uff0c\u5e03\u5c40\u65e0\u6cd5\u586b\u5145\u6ee1\n    private void init10() {\n        ViewMyLayoutBinding binding = ViewMyLayoutBinding.inflate(LayoutInflater.from(getContext()));\n        addView(binding.getRoot());\n    }\n\n    private void init11() {\n        ViewMyLayoutBinding binding = ViewMyLayoutBinding.inflate(LayoutInflater.from(getContext()), this, true);\n    }\n\n    private void init12() {\n        ViewMyLayoutBinding binding = ViewMyLayoutBinding.inflate(LayoutInflater.from(getContext()), this, false);\n        addView(binding.getRoot());\n    }\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u7684layout\u6587\u4ef6\u6839\u6807\u7b7e\u4e3amerge<\/h4>\n<p>\u6211\u4eec\u6dfb\u52a0\u4e00\u4e2a<code>view_my_layout_merge.xml<\/code>\u6587\u4ef6\uff0c\u6839\u6807\u7b7e\u4e3amerge\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;merge xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;match_parent&quot;&gt;\n\n    &lt;TextView\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;match_parent&quot;\n        android:gravity=&quot;center&quot;\n        android:text=&quot;\u8fd9\u662f\u81ea\u5b9a\u4e49merge&quot;\n        android:textSize=&quot;50sp&quot; \/&gt;\n\n&lt;\/merge&gt;<\/code><\/pre>\n<p>\u6b64\u65f6\u5728<code>MyLinearLayout.java<\/code>\u4e2d\u4f7f\u7528\u7684\u8bdd\uff0c\u6b63\u786e\u5199\u6cd5\u662f<code>init20()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">private void init20() {\n    ViewMyLayoutMergeBinding binding = ViewMyLayoutMergeBinding.inflate(LayoutInflater.from(getContext()), this);\n}\n\n\/\/\u6ca1\u6709\u6548\u679c\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u8fd8\u6ca1\u6709rootView\nprivate void init21() {\n    ViewMyLayoutMergeBinding binding = ViewMyLayoutMergeBinding.bind(this);\n}<\/code><\/pre>\n<p>\u6211\u4eec\u5bf9\u6bd4\u4e0b\u4f7f\u7528<code>merge<\/code>\u6807\u7b7e\u548c\u4e0d\u4f7f\u7528<code>merge<\/code>\u6807\u7b7e\u6240\u5bf9\u5e94\u7684Binding\u6587\u4ef6\uff1a<\/p>\n<p>\u4f7f\u7528<code>merge<\/code>\u6807\u7b7e\u751f\u6210\u7684\u4ee3\u7801\u5927\u81f4\u5982\u4e0b\uff0c<code>inflate()<\/code>\u65b9\u6cd5\u6700\u7ec8\u8c03\u7528\u4e86<code>bind()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">@NonNull\npublic static ViewMyLayoutMergeBinding inflate(@NonNull LayoutInflater inflater,\n    @NonNull ViewGroup parent) {\n    if (parent == null) {\n        throw new NullPointerException(&quot;parent&quot;);\n    }\n    inflater.inflate(R.layout.view_my_layout_merge, parent);\n    return bind(parent);\n}\n\n@NonNull\npublic static ViewMyLayoutMergeBinding bind(@NonNull View rootView) {\n    if (rootView == null) {\n        throw new NullPointerException(&quot;rootView&quot;);\n    }\n    return new ViewMyLayoutMergeBinding(rootView);\n}<\/code><\/pre>\n<p>\u4e0d\u4f7f\u7528<code>merge<\/code>\u6807\u7b7e\u7684Binding\u4ee3\u7801\u5982\u4e0b\uff0c<code>inflate(@NonNull LayoutInflater inflater)<\/code>\u8c03\u7528\u4e86<code>inflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent, boolean attachToParent)<\/code>\u65b9\u6cd5\uff0c\u6700\u7ec8\u8c03\u7528\u4e86<code>bind(@NonNull View rootView)<\/code>\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">@NonNull\npublic static ViewMyLayoutBinding inflate(@NonNull LayoutInflater inflater) {\n    return inflate(inflater, null, false);\n}\n\n@NonNull\npublic static ViewMyLayoutBinding inflate(@NonNull LayoutInflater inflater,\n    @Nullable ViewGroup parent, boolean attachToParent) {\n    View root = inflater.inflate(R.layout.view_my_layout, parent, false);\n    if (attachToParent) {\n        parent.addView(root);\n    }\n    return bind(root);\n}\n\n@NonNull\npublic static ViewMyLayoutBinding bind(@NonNull View rootView) {\n    if (rootView == null) {\n        throw new NullPointerException(&quot;rootView&quot;);\n    }\n    return new ViewMyLayoutBinding((ConstraintLayout) rootView);\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u57fa\u672c\u5c31\u628a\u6240\u6709\u7684\u81ea\u5b9a\u4e49\u89c6\u56fe\u4e2d\u4f7f\u7528ViewBinding\u7684\u65b9\u6cd5\u603b\u7ed3\u4e86\u4e00\u4e0b\uff0c\u4e3b\u8981\u662finflate\u65b9\u6cd5\u7684\u4f7f\u7528\uff0c\u5176\u5b9e\u5c31\u662f\u5e2e\u6211\u4eec\u5c01\u88c5\u4e86\u4e0binflate\u65b9\u6cd5\uff0c\u5982\u679c\u4e0d\u77e5\u9053\u4f7f\u7528\u54ea\u4e2a\u65b9\u6cd5\u7684\u8bdd\u53ef\u4ee5\u67e5\u770b\u751f\u6210\u7684ViewBinding\u6e90\u4ee3\u7801\uff0c\u4e00\u773c\u5c31\u80fd\u660e\u4e86\u6211\u4eec\u4e4b\u524d\u7684\u5199\u6cd5\u5bf9\u5e94\u7684\u662f\u73b0\u5728\u7684\u54ea\u4e2a\u65b9\u6cd5\u3002<\/p>\n<h3>Adapter\u4e2d\u4f7f\u7528ViewBinding<\/h3>\n<p>\u5728RecyclerView\u7ed3\u5408Adapter\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u518d\u4f7f\u7528ViewBinding\u6765\u5c1d\u8bd5\u4e0b\uff0c\u76f4\u63a5\u8d34Adapter\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">public class MainAdapter extends RecyclerView.Adapter&lt;MainAdapter.ViewHolder&gt; {\n\n    private List&lt;String&gt; mList;\n\n    public MainAdapter(List&lt;String&gt; list) {\n        mList = list;\n    }\n\n    @NonNull\n    @Override\n    public MainAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {\n        \/\/\u4e4b\u524d\u7684\u5199\u6cd5\n        \/\/View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_comment, parent, false);\n        \/\/ViewHolder holder = new ViewHolder(view);\n\n        \/\/\u4f7f\u7528ViewBinding\u7684\u5199\u6cd5\n        LayoutCommentBinding commentBinding = LayoutCommentBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);\n        ViewHolder holder = new ViewHolder(commentBinding);\n        return holder;\n    }\n\n    @Override\n    public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int position) {\n        holder.mTextView.setText(mList.get(position));\n    }\n\n    @Override\n    public int getItemCount() {\n        return mList.size();\n    }\n\n    static class ViewHolder extends RecyclerView.ViewHolder {\n\n        TextView mTextView;\n\n        \/\/\u4e4b\u524d\u7684\u5199\u6cd5\n        \/\/public ViewHolder(@NonNull View itemView) {\n        \/\/    super(itemView);\n        \/\/    mTextView = itemView.findViewById(R.id.tv_include);\n        \/\/}\n\n        \/\/\u4f7f\u7528ViewBinding\u7684\u5199\u6cd5\n        ViewHolder(@NonNull LayoutCommentBinding commentBinding) {\n            super(commentBinding.getRoot());\n            mTextView = commentBinding.tvInclude;\n        }\n    }\n}<\/code><\/pre>\n<p>\u53ea\u9700\u8981\u6ce8\u610f\u4e24\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><code>ViewHolder<\/code>\u7684\u6784\u9020\u5668\u53c2\u6570\u6539\u4e3a\u4f7f\u7528\u7684<code>Binding<\/code>\u5bf9\u8c61<\/li>\n<li>\u5b9e\u4f8b\u5316<code>ViewHolder<\/code>\u7684\u65f6\u5019\u4f20\u5165\u76f8\u5e94\u7684<code>Binding<\/code>\u5bf9\u8c61<\/li>\n<\/ul>\n<h2>\u5173\u4e8e\u5c01\u88c5<\/h2>\n<p>\u5927\u6982\u4e86\u89e3\u4e86ViewBinding\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5c06\u5176\u5b8c\u5168\u5c01\u88c5\u5728BaseActivity\uff08BaseFragment\u3001BaseDialog\u3001BaseView\u7b49\uff09\u7b49\u5e95\u5c42\u7684\u516c\u5171\u7c7b\u4e2d\uff0c\u7701\u53bb\u624b\u52a8\u5b9e\u4f8b\u5316\u76f8\u5e94ViewBinding\u7c7b\u7684\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<p>\u9996\u5148\u53ef\u4ee5\u4f7f\u7528\u6cdb\u578b\u7c7b\uff0c\u6bcf\u4e2a\u5177\u4f53\u7684Activity\u7ee7\u627fBaseActivity\uff0c\u5e76\u4f20\u9012\u8fdb\u53bb\u5bf9\u5e94\u7684ViewBinding\uff0c\u7136\u540e\u53cd\u5c04\u5bf9\u5e94\u7684<code>inflate()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5230ViewBinding\u5b9e\u4f8b\u3002\u62ff\u5230\u5b9e\u4f8b\u540e\u5c31\u53ef\u4ee5\u5bf9\u63a7\u4ef6\u4e3a\u6240\u6b32\u4e3a\u4e86\u4e0d\u662f\uff01\uff01<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u4f7f\u7528ViewBinding\u7684\u8bdd\uff0c\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u65b0\u5efa<code>xxx.xml<\/code>\u5e03\u5c40\u540e\u5c31\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5bf9\u5e94\u7684<code>xxxBinding.java<\/code>\u7684\u6587\u4ef6\uff0c\u5b9e\u4f8b\u5316<code>xxxBinding<\/code>\u53ea\u9700\u8981\u8c03\u7528\u5b83\u81ea\u8eab\u7684<code>inflate()<\/code>\u65b9\u6cd5\u5373\u53ef\u3002<br \/>\n\u6ce8\u610f\u4e0d\u540c\u60c5\u51b5\u4e0b\u4f7f\u7528\u4e0d\u540c\u7684<code>inflate()<\/code>\u65b9\u6cd5\uff0c\u4ee5\u53ca\u4f7f\u7528\u4e86<code>merge<\/code>\u6807\u7b7e\u60c5\u51b5\u4e0b\u7684<code>bind()<\/code>\u65b9\u6cd5\uff0c\u4ee5\u53ca\u4f7f\u7528<code>merge<\/code>\u6807\u7b7e\u5e03\u5c40\u548c\u5176\u4ed6\u6b63\u5e38<code>xxxLayout<\/code>\u5e03\u5c40\u6240\u4ea7\u751f\u7684\u4e0d\u540c\u7684<code>inflate()<\/code>\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u968f\u7740Android Studio 3.6\u7684\u6b63\u5f0f\u53d1\u5e03\uff0cGradle\u63d2\u4ef6\u5347\u7ea7\u52303.6.0\uff0cButterKni [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[412],"class_list":["post-1642","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-viewbinding"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1642"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1642\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}