Kotlin中使用DataBinding的简单实现

1.配置如下:

Project build_gradle 

buildscript {
    ext.kotlin_version = '1.2.21'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

app build_gradle 

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
... ...
dataBinding {
        enabled true
    }
}
kapt {
    generateStubs = true
}
dependencies {
//DataBind
    kapt "com.android.databinding:compiler:2.3.0"
}

2. 使用如下:

    2.1 在Activity中使用

DataBindingUtil.setContentView(this@MainActivity, R.layout.activity_main)

mContentView.userInfo = UserInfo("马齐", "18", "188")

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

    <data>

        <import type="com.apicloud.pkg.sdk.data.UserInfo" />
      
        <variable
            name="userInfo"
            type="UserInfo" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{userInfo.name}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{userInfo.age}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{userInfo.higeht}" />

        <FrameLayout
            android:layout_width="match_parent"
            android:id="@+id/main_container"
            android:layout_height="match_parent"></FrameLayout>
    </LinearLayout>
</layout>   

2.2 在Fragment中使用

fun toInit() {
    mContentView.endModel = EndModel(Calendar.getInstance().timeInMillis,
            "点我有惊喜", false)
    mContentView.imageUrl = "http://images.csdn.net/20150810/Blog-Image%E5%89%AF%E6%9C%AC.jpg"
    mContentView.changeTime.onClick {
        mContentView.endModel.time = Calendar.getInstance().timeInMillis
        mContentView.endModel.showRed = !mContentView.endModel.showRed
        mContentView.endModel.showView.set(!mContentView.endModel.showView.get())
    }
  //我使用的是IRecyclerView库 看起来是不是很简单?
    mContentView.layoutManager = LinearLayoutManager(context)
    val baseReclyerViewAdapter = object : CommonRecycleViewAdapter<PageModel>(context, R.layout.item_page) {
        override fun convert(helper: ViewHolderHelper?, t: PageModel?, position: Int) {

        }
    }
    mContentView.adapter = baseReclyerViewAdapter
    for (i in 1..10) {
        baseReclyerViewAdapter.add(PageModel())
    }
}
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
    <!--这个和 java 中使用一样,导入 View 类 可使用类方法-->
        <import type="android.view.View" />
    <!--可以理解为 Bean 对象-->
        <import type="com.apicloud.pkg.sdk.data.EndModel" />
    <!--可使用 Utils 的类方法-->
        <import type="com.apicloud.pkg.sdk.utils.UtilsKt" />
    <!--声明变量名 Databinding会自动生成 set 和 get 方法-->    
        <variable
        name="endModel"
        type="EndModel" />
    <!--声明变量名 Databinding会自动生成 set 和 get 方法-->    
        <variable
            name="adapter"
            type="android.support.v7.widget.RecyclerView.Adapter" />
    <!--声明变量名 Databinding会自动生成 set 和 get 方法-->
        <variable
            name="layoutManager"
            type="android.support.v7.widget.RecyclerView.LayoutManager" />
    <!--声明变量名 Databinding会自动生成 set 和 get 方法-->
        <variable
            name="imageUrl"
            type="String" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <!--根据showView的值显示隐藏  注意:不要使用isTrue 这样的变量名 编译时会出现true的关键词冲突-->
        <TextView
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="@{endModel.showView?View.VISIBLE:View.GONE}" />
    <!--这是字符串拼接操作 String.valueOf()为Long转换字符串操作--> 
        <!-- next.visibility 为跟随 next 布局显示和隐藏  
    注意:一定要在next下申明 不然编译报错 这种局限可用RelativeLayout解决-->
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{@string/nameFormat(endModel.result,String.valueOf(endModel.time))}"
            android:visibility="@{next.visibility}"></TextView>
    <!--使用 Utils 的静态方法 将 Long转换成日期(String)类型-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{UtilsKt.long2Data(endModel.time)}"></TextView>
    <!--根据 shwoRed 显示不同的颜色-->
        <Button
            android:id="@+id/change_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@{endModel.showRed?@color/color_999999 : @color/color_FF244F}"
            android:text="@{endModel.result}" />
    <!-- 注意:app:image 此为DataBinding的自定义属性 -->
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:image="@{imageUrl}" />
    <!-- 说实话没搞明白 此 app:adapter app:layoutManager 的任何修改 都会报编译异常 
    应该是查找不到 改类的setAdapter() 以及 setLayoutManager()方法  -->
        <com.aspsine.irecyclerview.IRecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:adapter="@{adapter}"
            app:layoutManager="@{layoutManager}" />
    </LinearLayout>
</layout>
 <string name="nameFormat">%s, %s</string>//组合字符串
 
 
<color name="color_999999">#999999</color>
<color name="color_FF244F">#FF244F</color>
<color name="color_E5E5E5">#e5e5e5</color>

Utils.kt

fun long2Data(data: Long): String = SimpleDateFormat("yyyy-MM-dd").format(Date(data))

BindingUtil.kt

这个BindingUtil 是可以随便名明的DataBinding会自动寻找到该方法

@BindingAdapter("bind:image")
fun ImageView.imageLoader(url: String) {
    Glide.with(context).load(url).into(this)
}
//这个BindingConversion真的不实用  如果参数为String或者Int类型将会造成 其他不想使用该方法的布局也被迫使用
不推荐
//@BindingConversion
//fun convertDate(date: Date): String {
//    return SimpleDateFormat("yyyy-MM-dd").format(date)
//}

最后 在我GitHub上有源代码  https://github.com/goodluckforme/APiCloudDemo

自己学习也方便大家。

持续更新中...


PS:这是基于我的MVP模板写的,别忘了我的一键 MVP快速开发神器,欢迎来Start

GitHub https://github.com/goodluckforme/muc_mvp/tree/second

Blog http://blog.csdn.net/qq_20330595/article/details/79269581



猜你喜欢

转载自blog.csdn.net/qq_20330595/article/details/79358570