美文网首页
Data Binding基础了解一下

Data Binding基础了解一下

作者: 为什么要简称 | 来源:发表于2018-11-23 14:33 被阅读12次

在Kotlin中使用dataBinding时发生了unresolved reference Binding
网上查了查需要配置个东西:

kapt "com.android.databinding:compiler:$android_plugin_version"

要配置这个东西
project中的build.gradle:

ext.android_plugin_version = '2.2.0-alpha4'
dependencies{
    ...
    classpath "com.android.tools.build:gradle:$android_plugin_version"
}

app的build.gradle中:

apply plugin: 'kotlin-kapt'
android{...}
kapt {
    generateStubs = true
}

原文:https://www.androidhive.info/android-working-with-databinding/#enable-databinding

一、授权

app/build.gradle
android {
    dataBinding {
        enabled = true
    }

    compileSdkVersion 27

    defaultConfig {
        applicationId "info.androidhive.databinding"
        minSdkVersion 16
        // ..
    }
}

二、基本示例

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>
     
    <variable
        name="user"
        type="info.androidhive.databinding.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:orientation="vertical"
    android:padding="@dimen/fab_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <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="@{user.email}" />

</LinearLayout>
</layout>

三、略

四、DataBinding在<include>标签中的使用
activity_main.xml

<include
        android:id="@+id/content"
        layout="@layout/content_main"
        bind:user="@{user}" />

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

    <variable
        name="user"
        type="info.androidhive.databinding.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:orientation="vertical"
    android:padding="@dimen/fab_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <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="@{user.email}" />

</LinearLayout>
</layout>

五、绑定 Click Listener / Event Handling

public class MyClickHandlers {

    public void onFabClicked(View view) {
        Toast.makeText(getApplicationContext(), "FAB clicked!", Toast.LENGTH_SHORT).show();
    }

}

布局中先导入实例

<layout xmlns:bind="http://schemas.android.com/apk/res/android">

<data>

    <variable
        name="handlers"
        type="info.androidhive.databinding.MainActivity.MyClickHandlers" />
</data>

<android.support.design.widget.CoordinatorLayout ...>

    <android.support.design.widget.FloatingActionButton
        ...
        android:onClick="@{handlers::onFabClicked}" />

</android.support.design.widget.CoordinatorLayout>
</layout>

管理事件的类

public class MyClickHandlers {

    Context context;

    public MyClickHandlers(Context context) {
        this.context = context;
    }

    public void onFabClicked(View view) {
        Toast.makeText(getApplicationContext(), "FAB clicked!", Toast.LENGTH_SHORT).show();
    }

    public void onButtonClick(View view) {
        Toast.makeText(getApplicationContext(), "Button clicked!", Toast.LENGTH_SHORT).show();
    }

    public void onButtonClickWithParam(View view, User user) {
        Toast.makeText(getApplicationContext(), "Button clicked! Name: " + user.name, Toast.LENGTH_SHORT).show();
    }

    public boolean onButtonLongPressed(View view) {
        Toast.makeText(getApplicationContext(), "Button long pressed!", Toast.LENGTH_SHORT).show();
        return false;
    }
}

布局中

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

    <variable
        name="user"
        type="info.androidhive.databinding.User" />

    <variable
        name="handlers"
        type="info.androidhive.databinding.MainActivity.MyClickHandlers" />
</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:orientation="vertical"
    android:padding="@dimen/fab_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <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="@{user.email}" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="@{handlers::onButtonClick}"
        android:text="CLICK" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="@{(v) -> handlers.onButtonClickWithParam(v, user)}"
        android:text="CLICK WITH PARAM" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LONG PRESS"
        android:onLongClick="@{handlers::onButtonLongPressed}" />

</LinearLayout>
</layout>

六、使用Observables更新UI

public class User extends BaseObservable {
String name;
String email;

@Bindable
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
    notifyPropertyChanged(BR.name);
}

@Bindable
public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
    notifyPropertyChanged(BR.email);
}
}

注:寡人在用BR类时发生了死不认账的情况,重启治百病。

七、使用ObservableFields更新UI

如果不想更新所有fields,可以指定具体字段进行更新

public class User {
public static ObservableField<String> name = new ObservableField<>();
public static ObservableField<String> email = new ObservableField<>();

public ObservableField<String> getName() {
    return name;
}

public ObservableField<String> getEmail() {
    return email;
}
}

重新设置值

public void onFabClicked(View view) {
    user.name.set("Ravi");
    user.email.set("ravi8x@gmail.com");
}

注:示例中使用的是静态属性。

八、加载图片(Glide、Picasso)

String profileImage;

public String getProfileImage() {
    return profileImage;
}

public void setProfileImage(String profileImage) {
    this.profileImage = profileImage;
}

@BindingAdapter({"android:profileImage"})
public static void loadImage(ImageView view, String imageUrl) {
    Glide.with(view.getContext())
            .load(imageUrl)
            .into(view);

    // If you consider Picasso, follow the below
    // Picasso.with(view.getContext()).load(imageUrl).placeholder(R.drawable.placeholder).into(view);
}

注:不要忘记加权限。

<ImageView
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:layout_marginTop="@dimen/fab_margin"
 android:profileImage="@{user.profileImage}" />

九、绑定函数

<ImageView
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:layout_marginTop="@dimen/fab_margin"
 android:profileImage="@{user.profileImage}" />

布局中先import

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>
    <import type="info.androidhive.databinding.BindingUtils" />
</data>

<LinearLayout ...>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{BindingUtils.capitalize(user.name)}" />

</LinearLayout>
</layout>

相关文章

网友评论

      本文标题:Data Binding基础了解一下

      本文链接:https://www.haomeiwen.com/subject/vxpdqqtx.html