#视图绑定#
1.将 viewBinding 元素添加到其 build.gradle 文件中的android中,如下所示
viewBinding {
enabled = true
}
dataBinding {
enabled = true
}
2.#Activity视图绑定#
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<import type="java.util.List"/>
<import type="java.util.Map"/>
<variable
name="user"
type="com.bawei.zhuangaoer1110.User" />
<variable
name="view"
type="android.view.View" />
<variable
name="list"
type="List<String>" />
<variable
name="index"
type="int" />
<variable
name="map"
type="Map<String,String>" />
<variable
name="key"
type="String" />
<variable
name="presenter"
type="com.bawei.zhuangaoer1110.Presenter" />
<variable
name="userinfo"
type="com.bawei.zhuangaoer1110.UserInfo" />
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(user.age)}"
android:visibility="@{user.age>50?view.GONE:view.VISIBLE}"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/input_et"></EditText>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{inputEt.text}"
android:id="@+id/input_tv"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{list.get(index)}"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{map.get(key)}"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="事件绑定"
android:onClick="@{()->presenter.onClick()}"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{userinfo.name}">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{presenter.firstName}"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可观察数据对象"
android:onClick="@{()->presenter.onSaveClick(userinfo)}"
></TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tou"></ImageView>
</LinearLayout>
</layout>
3.
#Fragment视图绑定#
public class oneFragment extends Fragment {
FragmentOneBinding fragmentOneBinding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// View view= inflater.inflate(R.layout.fragment_one, container, false);
fragmentOneBinding=FragmentOneBinding.inflate(inflater,container,false);
View view=fragmentOneBinding.getRoot();
return view;
}
}
4.静态 嵌套Fragment
//创建fragment管理器 FragmentManager fragmentManager=getSupportFragmentManager(); //创建事务 FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); //fragment,创建,添加,删除,替换 fragmentTransaction.replace(R.id.lm,new BlankFragment1()); //提交 fragmentTransaction.commit();
2569




被折叠的 条评论
为什么被折叠?



