Use of DataBinding

  A recent project uses the MVVM framework, because it hasn't been used before, it took a little time to use it for the first time. The idea of ​​the framework itself is not difficult to understand, but if you want to develop smoothly in this framework, the most basic prerequisite for the proficient use of DataBinding. Not much to say, today I will summarize the common binding usage.

Simply say DataBinding use process:

android {
  
    dataBinding { enabled true }
    
}

1. Select the root layout, Alt+Enter

Then it became like this

 

2. Create a Binding object (edit, this Binding object will be generated in the project, for example: activity_main_mvvm remove the capital letter +Binding after _)

in activity

ActivityMainMvvmBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main_mvvm);

Item layout in fragment or Adapter binding

FragmentMainMvvmBinding  binding 
= DataBindingUtil.inflate(inflater, R.layout.fragment_main_mvvm, container, false);

In this way, you can find the controls in the layout through this object, such as R.id.tv_name in the layout; binding.tvName in the java code; save the findViewById

 

3. Associate the entity Enity with the layout, so that the data of the entity object in the layout can be used directly, that is, the purpose of binding is achieved.

<data>
   
    <variable
        name="userInfo"
        type="com.example.laoliu.mytest1.UserInfoEnity" />

</data>

 

1. Binding data

1. Simple data binding (a field in the entity is directly displayed)

<TextView
    android:id="@+id/tv_name"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#e9b1b1"
    android:gravity="center"
    android:text="@{userInfo.name}" /> <TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#e9b1b1"
android:gravity="center"
android:text="@{userInfo.name,default=`游客`}" />

2. If the control does not directly display the content of the enity field, it displays different texts according to different values. It can be roughly divided into the following situations:

     Case 1: Customizing attributes for a field means defining attributes in the enity class. (A certain field of enity has many types and rich meaning, which is difficult to express in xml or expression in xml will lead to poor legibility)

//根据userType  显示不同的用户类型。value对应的值:自定义属性。第一个参数:控件。第二个参数:绑定的数据。

Enity中:
@BindingAdapter(value = {"userTypeShow"}, requireAll = false)
public static void setUserName(TextView textView, String userType) {
    if (userType != null) {
        switch (userType) {
            case "0":
                textView.setText("游客");
            case "1":
                textView.setText("普通用户");
                break;
            case "2":
                textView.setText("VIP用户");
                break;
        }
    }
}

xml use:

<TextView
    android:id="@+id/tv_userType"
    android:layout_width="wrap_content"
    android:layout_height="17dp"
    android:layout_marginLeft="10dp"
    android:background="@drawable/level_bg"
    android:gravity="center"
    android:textColor="#FFFFFFFF"
    android:textSize="12sp"
    android:visibility="gone"
    app:userTypeShow="@{userInfo.userType}" />

另外ImageView加载在线url图片,也可用此方法处理。
情况二:将网络请求返回的实体进行再处理,将不能直接展示的字段用其他的字段代替,以使控件可以直接展示。这里不做示例。

2. Ternary operator

1.boolean

android:visibility="@{userInfo.checked!=null? View.VISIBLE:View.GONE}"   //字段本来的属性
public boolean isChecked() {   //对某个字段处理然后在获取
    if (!Utils.isEmpty(isRealNameCheck)) {
        return true;
    }
    return false;
}   

Use directly in xml

android:visibility="@{userInfo.isChecked? View.VISIBLE:View.GONE}"

 

2.int

 android:text="@{userInfo.userName==0? userInfo.userName: `游客`}"
 android:text="@{userInfo.userName==1? userInfo.userName: @string/redacted }"

3. Non-empty processing  

 android:text="@{userInfo.userName?? @string/redacted }"
 相当于
 android:text="@{userInfo.userName==null? userInfo.userName: @string/redacted }"

 

4. Other 

 android:padding="@{isBig ? @dimen/bigPadding : @dimen/smallPadding}"
 Format字符串格式:
 android:text="@{@string/nameFormat(firstName, lastName)}"
<Button android:onClick="@{isManager ? handlers.managerClick : handlers.managerClick}"/>



Three click event binding

//无参数
public void onItemClick0() {
    Log.e("TAG", "onItemClick1");
}

//一个参数 View
public void onItemClick1(View view) {
    Log.e("TAG", "onItemClick1:view");
}

//两个参数 public void onItemClick1(String userName) {
   Log.e("TAG", "onItemClick1:userName"); 
 }

//两个参数
public void onItemClick2(View view, String userName) {
    Log.e("TAG", "onItemClick1");
    Toast.makeText(view.getContext(), userName, Toast.LENGTH_LONG).show();
}

xml:

@{() -> clickEvent.onItemClick0()}  onItemClick0()//onItemClick0()

android:onClick="@{clickevent.onItemClick1}"
相当于android:onClick="@{clickevent::onItemClick1}" //onItemClick1(View view)

@{() -> clickEvent.onItemClick1(userInfo.userName)}  //onItemClick1(String userName)
@{(view) -> clickEvent.onItemClick1(userInfo.userName)} //onItemClick2(View view, String userName)

注意:字符串`` 表示, 或者@string/**

对于使用View.Visible 需要导包 集合之类的也需要 

For the two parameters to import the package  <variable name="view" type="View"/>

 

 

 

 

Guess you like

Origin blog.csdn.net/lk2021991/article/details/100052308