@zifeng328573112
2021-03-16T08:37:50.000000Z
字数 3973
阅读 159
在模块中启用视图绑定之后,系统会为该模块中的每个 XML 布局文件生成一个绑定类。绑定类的实例包含对在相应布局中具有 ID 的所有视图的直接引用。通过视图绑定功能,可以替代findViewById,从而更轻松地编写可与视图交互的代码。
视图绑定在 Android Studio 3.6 Canary 11 及更高版本中可用。
android {
viewBinding {
enabled = true
}
}
<LinearLayout
tools:viewBindingIgnore="true" >
</LinearLayout>
为某个模块启用视图绑定功能后,系统会为该模块中包含的每个XML布局文件各生成一个绑定类。每个绑定类均包含对根视图以及具有ID的所有视图的引用。绑定类的名称为:XML文件的名称转换为驼峰式大小写+Binding。
XML文件: 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_hello_world"
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" />
</androidx.constraintlayout.widget.ConstraintLayout>
自动生成绑定类文件:
ActivityBleBinding.java
public final class ActivityBleBinding implements ViewBinding {
@NonNull
private final RelativeLayout rootView;
@NonNull
public final FloatingActionButton floatingButton;
@NonNull
public final LinearLayout llAdapterTip;
@NonNull
public final RecyclerView recyclerView;
@NonNull
public final SwipeRefreshLayout swipeLayout;
@NonNull
public final TextView tvAdapterStates;
@NonNull
public final TextView tvTip;
private ActivityBleBinding(@NonNull RelativeLayout rootView,
@NonNull FloatingActionButton floatingButton, @NonNull LinearLayout llAdapterTip,
@NonNull RecyclerView recyclerView, @NonNull SwipeRefreshLayout swipeLayout,
@NonNull TextView tvAdapterStates, @NonNull TextView tvTip) {
this.rootView = rootView;
this.floatingButton = floatingButton;
this.llAdapterTip = llAdapterTip;
this.recyclerView = recyclerView;
this.swipeLayout = swipeLayout;
this.tvAdapterStates = tvAdapterStates;
this.tvTip = tvTip;
}
@Override
@NonNull
public RelativeLayout getRoot() {
return rootView;
}
@NonNull
public static ActivityBleBinding inflate(@NonNull LayoutInflater inflater) {
return inflate(inflater, null, false);
}
@NonNull
public static ActivityBleBinding inflate(@NonNull LayoutInflater inflater,
@Nullable ViewGroup parent, boolean attachToParent) {
View root = inflater.inflate(R.layout.activity_ble, parent, false);
if (attachToParent) {
parent.addView(root);
}
return bind(root);
}
@NonNull
public static ActivityBleBinding bind(@NonNull View rootView) {
// The body of this method is generated in a way you would not otherwise write.
// This is done to optimize the compiled bytecode for size and performance.
int id;
missingId: {
id = R.id.floatingButton;
FloatingActionButton floatingButton = rootView.findViewById(id);
if (floatingButton == null) {
break missingId;
}
id = R.id.ll_adapter_tip;
LinearLayout llAdapterTip = rootView.findViewById(id);
if (llAdapterTip == null) {
break missingId;
}
id = R.id.recyclerView;
RecyclerView recyclerView = rootView.findViewById(id);
if (recyclerView == null) {
break missingId;
}
id = R.id.swipeLayout;
SwipeRefreshLayout swipeLayout = rootView.findViewById(id);
if (swipeLayout == null) {
break missingId;
}
id = R.id.tv_adapter_states;
TextView tvAdapterStates = rootView.findViewById(id);
if (tvAdapterStates == null) {
break missingId;
}
id = R.id.tv_tip;
TextView tvTip = rootView.findViewById(id);
if (tvTip == null) {
break missingId;
}
return new ActivityBleBinding((RelativeLayout) rootView, floatingButton, llAdapterTip,
recyclerView, swipeLayout, tvAdapterStates, tvTip);
}
String missingId = rootView.getResources().getResourceName(id);
throw new NullPointerException("Missing required view with ID: ".concat(missingId));
}
}
该类实现了ViewBinding接口,: ViewBinding.java
/** A type which binds the views in a layout XML to fields. */
public interface ViewBinding {
/**
* Returns the outermost {@link View} in the associated layout file. If this binding is for a
* {@code <merge>} layout, this will return the first view inside of the merge tag.
*/
@NonNull
View getRoot();
}