使用dataBinding,真正不用findViewById
来源:互联网 发布:结构设计软件有哪些 编辑:程序博客网 时间:2024/06/04 18:39
1.在工程的build.gradle文件的android节点添加dataBinding,属性值为:enabled = true如下:android { ...... //AS 1.3才可以使用 dataBinding { enabled = true } ...... }2.布局文件中声明:<layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="user" type="com.example.lj.ljplayerhd.main.personal.Login.model.User" /> </data> <LinearLayout 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" android:gravity="center" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <ScrollView android:id="@+id/register_form" android:layout_width="match_parent" android:layout_height="488dp"> <LinearLayout android:id="@+id/email_login_form" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <AutoCompleteTextView android:id="@+id/register_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/prompt_email" android:inputType="textEmailAddress" android:maxLines="1" android:singleLine="true" android:text="@{user.userName}" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:counterMaxLength="10" app:counterOverflowTextAppearance="@android:style/TextAppearance.DeviceDefault.Small" app:hintAnimationEnabled="true" app:passwordToggleEnabled="true"> <EditText android:id="@+id/register_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/register_password" android:imeActionId="@+id/login" android:imeActionLabel="@string/action_sign_in_short" android:imeOptions="actionUnspecified" android:inputType="textPassword" android:maxLines="1" android:singleLine="true" android:text="@{user.passWord}" /> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/register_button" style="?android:textAppearanceSmall" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="@string/register" android:textStyle="bold" /> <Button android:id="@+id/regiter_return_login" style="?android:textAppearanceSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/register_retrun_login" android:textStyle="bold" /> </LinearLayout> </ScrollView> </LinearLayout></layout>这里需要注意的是布局文件是以:layout作为根节点。其中data节点就是我们需要绑定的数据,data节点中variable就是我们具体使用到的Model,name是我们自己定义的变量名(说白了就是看可以随便取名字),
type就是我们需要绑定的具体的类(这里可不能随便写)。
3.绑定数据:public class RegisterFragment extends BaseFragment implements LoginContract.RegisterFragmentView { private final int RETRUN_LOGIN = 1; private LoginContract.RegisterFragmentPresenter lrp; private Button retrunLogin; private EditText registerUserName; private EditText registerPassword; private EditText registerConfirmPassword; private Button registerButton; private FragmentRegisterBinding fragmentRegisterBinding; @Override public int getLayoutId() { return R.layout.fragment_register; } @Override public void initViews() { fragmentRegisterBinding = FragmentRegisterBinding.bind(root); User user = new User("tianluhua", "123456", "123456", true); fragmentRegisterBinding.setUser(user); //retrunLogin = (Button) root.findViewById(R.id.regiter_return_login); //registerUserName = (EditText) root.findViewById(R.id.register_email); //registerPassword = (EditText) root.findViewById(R.id.register_password); //registerConfirmPassword = (EditText) root.findViewById(R.id.register_confirm_password); //registerButton = (Button) root.findViewById(R.id.register_button); } @Override public void initEvents() { lrp = new RegisterPresenter(this, getActivity()); retrunLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { RxBus.getInstance().send(RETRUN_LOGIN); } }); }}3.1其中FragmentRegisterBinding这个类是系统根据上一步的XMl布局文件自动生成的,名字的规则是对应布局文件的名字(有下划线的话去掉下划线),
接下来就用这个类的静态方法得到对象:fragmentRegisterBinding = FragmentRegisterBinding.bind(root);这里本来该将刚刚的布局文件用LayoutInflate实例化成一个View传进去的,我这里在Fragment的父类中已经将布局文件实例化好了,名字就叫root而已。
3.2接下来就是设置Model数据了:User user = new User("tianluhua", "123456", "123456", true); fragmentRegisterBinding.setUser(user);new一个User的实例,设置给fragmentRegisterBinding。
效果如下:
0 0
- 使用dataBinding,真正不用findViewById
- DataBinding 再也不用写findviewbyId了
- Android使用DataBinding代替findViewById
- ButterKnife的使用,再也不用写findviewById
- 安卓日记——再也不用findViewById了(DataBinding快速入门)
- DataBinding祛除findviewbyid和数据绑定【一】
- DataBinding使用
- DataBinding使用
- DataBinding 使用
- DataBinding快速入门(还在用findViewById?)
- DataBinding快速入门(还在用findViewById?)
- DataBinding快速入门(还在用findViewById?)
- DataBinding快速入门(还在用findViewById?)
- Eclipse自动生成FindViewById,不用手写
- ButterKinfe:一个不用findViewById的神器
- android 自动初始化控件 不用注解不用findViewByid
- findViewById()使用常见错误
- LayoutInflater及findViewById使用
- 创建型、结构型、行为型模式
- Netty系列-简单示例
- 回调函数
- cpu 平均负载
- 腾讯微博项目
- 使用dataBinding,真正不用findViewById
- 杨辉三角问题
- 推荐系统:技术、评估及高效算法 第6章
- C++66
- js-词法结构
- [LeetCode] 330. Patching Array
- linux下c语言指针部分的一个总结
- asp.net传递的中文url参数在asp页面如何正确接收
- R语言-rjava安装(windows,linux)