如何在 Fragment 中使用视图绑定 请在 Fragment 的 onCreateView()方法中执行以下步骤(注意:Fragment 的存在时间比其视图长。请务必在 Fragment 的 onDestroyView() 方法中清除对绑定类实例的所有引用。)
调用生成的绑定类中包含的静态 inflate() 方法。此操作会创建该绑定类的实例以供 Fragment 使用。
通过调用 getRoot() 方法或使用 Kotlin 属性语法获取对根视图的引用。
从 onCreateView() 方法返回根视图,使其成为屏幕上的活动视图。
如果view已经创建可以在onViewCreated()中使用bind(view)方法。官方示例
//fragment_my.xml
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">
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="这是Fragment按钮"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:text="这是FragmentTextView"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
public class MyFragment extends Fragment {
private FragmentMyBinding binding;
public MyFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentMyBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.textView.setText("这是Fragment");
binding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("Fragment", "点击了按钮");
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
binding = null;
}
//在onViewCreated中使用View Binding
public class MyFragment extends Fragment {
private FragmentMyBinding binding;
public MyFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my,container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FragmentMyBinding binding = FragmentMyBinding.bind(view);
this.binding = binding;
binding.textView.setText("这是Fragment");
binding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("Fragment", "点击了按钮");
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
binding = null;
}
使用View Binding 写的基类
public class BaseActivity extends AppCompatActivity {
protected T viewBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
Class cls = (Class) type.getActualTypeArguments()[0];
try {
Method inflate = cls.getDeclaredMethod("inflate", LayoutInflater.class);
viewBinding = (T) inflate.invoke(null, getLayoutInflater());
setContentView(viewBinding.getRoot());
} catch (NoSuchMethodException | IllegalAccessException| InvocationTargetException e) {
e.printStackTrace();
}
}
}
//使用
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewBinding.button.setText("这是 MainActivity ViewBinding");
viewBinding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("MainView","点击按钮");
}
});
}
}
public class BaseFragment extends Fragment {
protected T viewBinding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
Class cls = (Class) type.getActualTypeArguments()[0];
try {
Method inflate = cls.getDeclaredMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
viewBinding = (T) inflate.invoke(null, inflater, container, false);
} catch (NoSuchMethodException | IllegalAccessException| InvocationTargetException e) {
e.printStackTrace();
}
return viewBinding.getRoot();
}
}
//使用
public class MainFragment extends BaseFragment{
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewBinding.button.setText("这是 MainFragment ViewBinding");
viewBinding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("MainView","点击按钮");
}
});
}
}