您的位置:首页 > 其它

使用dataBinding,真正不用findViewById

2016-12-05 23:31 543 查看
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"
a
4000
ndroid: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。

效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  DataBinding