ViewBindingを受け入れるための時間です!!
シェンジョウ過去ジャンク帆の何千も
先に春の木の万本の木。
-唐劉禹錫
I.はじめに
Androidのメーカー3.6の正式リリースでは、私は最前線の更新アーリーアダプターで義務がいます。AS Gradleのアップグレードリンクに再起動がアゴニストである後、あなたが3.5.1から3.6.0にアップグレードする必要があり、いつものようにスムーズにアップグレードしてください。案の定、問題!!
次のようにButterKnifeは、実際のエラー、ログは次のとおりです。
D:\xxx\libbase\component\dialog\BottomDialog.java:33: : Attempt to use @BindView for an already bound ID 0 on 'mTvNegative'. (com.xxx.libbase.component.dialog.BottomDialog.mLayoutContent)
ViewGroup mLayoutContent;
私は本当にああ困惑しています。、ButterKnifeをアップグレードし、それを解決する問題で、ソースコードを見て見つけるために、情報を裏返し、などなど。神は最終的に報わ、私はバージョンロールバック、そして穏やかにすべての戻りをGradleのだろう。[解決策がある場合には、感謝し、私に知らせてください]
第二に、知人のViewBinding
それButterKnife省略はfindViewById()は、重複コードの場合と同じです。実際には、2019年にGoogleの開発者サミットViewBindingが聞こえてきた、とコントロールIDのレイアウトを更新した直後に、より快適にするためにRとR2を区別する必要ButterKnifeよりもコンパイルすることが絶対に必要である活動、を参照することができます。
それを使用することですが、現実は、常に10物足りない、ViewBindingのうち9とても残酷で、ButterKnifeが唯一の第二選挙を思わ上記の3.6.0にアップグレードしてください。
第三に、抱擁のViewBinding
非常に詳細に書かれてViewBinding文書、公式について、参照結合のビューを。本論文で物事をシンプルには、公式のGoogleの下での主な問題のいくつかは言及していないと言います。
3.1環境要件
- アンドロイドStudioバージョン3.6以上
- Gradleプラグインバージョン3.6.0以上
3.2、オープンViewBinding機能
ViewBindingサポートがモジュールで有効になって、次のコードbuild.gradleファイルモジュールを追加します。
android {
...
viewBinding {
enabled = true
}
}
使用ViewBinding 3.3、アクティビティ
//之前设置视图的方法
setContentView(R.layout.activity_main);
//使用ViewBinding后的方法
mBinding = ActivityMainBinding.inflate(LayoutInflater.from(this));
setContentView(mBinding.getRoot());
あなたは、あなたのファイルのためViewBinding、activity_main.xmlを使用する場合、それは自動的にレイアウトファイルがあるActivityMainBinding.javaファイル(ビルド内のファイル/生成/ data_binding_base_class_source_out / XXX ...カタログ)を生成するのに役立ちます、見ることができますこぶ命名プラスサフィックスを結合、それは活動で直接使用されます。
3.3.1、直接コントロールのレイアウト
私たちは、レイアウトにIDを追加するとtv_textのTextView後、直接活動に使用されるmBinding.tvText制御を取得します。以下に示すように、また、取得したIDに制御ハンプ命名法に見られます。
mBinding.tvText.setText("是你得不到的ViewBinding");
3.3.2、インポートコントロールのレイアウト
例えば、我々はlayout_comment.xmlレイアウトが、レイアウトがID有するtv_includeのTextViewのを次のように、コードは次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_include"
android:text="这就是测试啊"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
そして、activity_main.xmlレイアウトファイルが含まれています。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
android:id="@+id/layout_include"
layout="@layout/layout_comment" />
</androidx.constraintlayout.widget.ConstraintLayout>
だから、どのように我々は、この時間を使用して行うlayout_comment.xmlすべての最初のラベルは、[アクティビティコードは次のように例のlayout_includeのために、IDを宣言する必要が含まれ、レイアウトのTextViewそれを制御します。
mBinding.layoutInclude.tvInclude.setText("这就是你的不对了");
それは驚くべきではない、非常に簡単ではありません。
注:
あなたが(例えば追加などのレイアウトlayout_comment.xmlのルートにIDを追加layout_xxxこの時に指定されている時にIDのを):
java.lang.NullPointerException: Missing required view with ID: layout_xxx