Skip to content

Commit 55bac7c

Browse files
committed
Add Android data binding sample
1 parent e1a2e02 commit 55bac7c

34 files changed

+663
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-kapt'
4+
5+
android {
6+
compileSdkVersion 25
7+
buildToolsVersion "25.0.3"
8+
defaultConfig {
9+
applicationId "org.example.kotlin.databinding"
10+
minSdkVersion 18
11+
targetSdkVersion 25
12+
versionCode 1
13+
versionName "1.0"
14+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15+
}
16+
buildTypes {
17+
release {
18+
minifyEnabled false
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
21+
}
22+
dataBinding {
23+
enabled = true
24+
}
25+
}
26+
27+
dependencies {
28+
compile fileTree(dir: 'libs', include: ['*.jar'])
29+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
30+
exclude group: 'com.android.support', module: 'support-annotations'
31+
})
32+
compile 'com.android.support:appcompat-v7:25.3.1'
33+
testCompile 'junit:junit:4.12'
34+
compile 'com.android.support.constraint:constraint-layout:1.0.1'
35+
compile "com.android.support:recyclerview-v7:25.3.1"
36+
kapt "com.android.databinding:compiler:$android_plugin_version"
37+
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
38+
compile 'com.github.bumptech.glide:glide:3.7.0'
39+
}
40+
repositories {
41+
mavenCentral()
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/4u7/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.example.kotlin.databinding;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("org.example.kotlin.databinding", appContext.getPackageName());
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="org.example.kotlin.databinding">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme">
14+
<activity android:name="org.example.kotlin.databinding.MainActivity">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
<activity android:name="org.example.kotlin.databinding.SecondActivity" />
22+
</application>
23+
24+
</manifest>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.example.kotlin.databinding
2+
3+
import android.databinding.BindingAdapter
4+
import android.widget.ImageView
5+
import com.bumptech.glide.Glide
6+
7+
object ImageBindingAdapter {
8+
@JvmStatic
9+
@BindingAdapter("android:src")
10+
fun setImageUrl(view: ImageView, url: String) {
11+
Glide.with(view.context).load(url).into(view)
12+
}
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.example.kotlin.databinding
2+
3+
import android.content.Intent
4+
import android.databinding.DataBindingUtil
5+
import android.os.Bundle
6+
import android.support.v7.app.AppCompatActivity
7+
import android.view.View
8+
import org.example.kotlin.databinding.databinding.ActivityMainBinding
9+
10+
11+
class MainActivity : AppCompatActivity() {
12+
13+
val weather = WeatherData(
14+
"Saint Petersburg",
15+
"10 °C",
16+
"Cloudy with rain and possible thunderstorms",
17+
"http://loremflickr.com/800/600/city")
18+
19+
var counter = 1
20+
21+
override fun onCreate(savedInstanceState: Bundle?) {
22+
super.onCreate(savedInstanceState)
23+
val binding: ActivityMainBinding =
24+
DataBindingUtil.setContentView(this, R.layout.activity_main)
25+
26+
binding.data = weather
27+
}
28+
29+
fun change(view: View) {
30+
++counter
31+
weather.temperature = "$counter °C"
32+
weather.imageUrl = "http://loremflickr.com/800/600/city?random=$counter"
33+
weather.notifyChange()
34+
}
35+
36+
fun startActivity(view: View) {
37+
startActivity(Intent(this, SecondActivity::class.java))
38+
}
39+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.example.kotlin.databinding
2+
3+
import android.databinding.ViewDataBinding
4+
import android.support.v7.app.AppCompatActivity
5+
import android.os.Bundle
6+
import android.support.v7.widget.LinearLayoutManager
7+
import android.support.v7.widget.RecyclerView
8+
import android.view.ViewGroup
9+
import android.view.LayoutInflater
10+
import android.databinding.DataBindingUtil
11+
12+
13+
class SecondActivity : AppCompatActivity() {
14+
15+
override fun onCreate(savedInstanceState: Bundle?) {
16+
super.onCreate(savedInstanceState)
17+
setContentView(R.layout.activity_second)
18+
19+
val items = listOf(
20+
WeatherData(
21+
"Saint Petersburg",
22+
"10 °C",
23+
"Cloudy with rain and possible thunderstorms",
24+
"http://loremflickr.com/800/600/city?random=2"),
25+
WeatherData(
26+
"Munich",
27+
"23 °C",
28+
"Partly sunny and delightful",
29+
"http://loremflickr.com/800/600/city?random=3"),
30+
WeatherData(
31+
"Novosibirsk",
32+
"9 °C",
33+
"Mostly cloudy",
34+
"http://loremflickr.com/800/600/city?random=4"))
35+
36+
val recyclerView = findViewById(R.id.weather_list) as RecyclerView
37+
recyclerView.layoutManager = LinearLayoutManager(this)
38+
recyclerView.adapter = WeatherAdapter(items)
39+
}
40+
41+
class WeatherAdapter(val data: List<WeatherData>): RecyclerView.Adapter<WeatherViewHolder>() {
42+
override fun onBindViewHolder(holder: WeatherViewHolder, position: Int) {
43+
holder.bind(data[position])
44+
}
45+
46+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WeatherViewHolder {
47+
val layoutInflater = LayoutInflater.from(parent.context)
48+
val binding: ViewDataBinding =
49+
DataBindingUtil.inflate(layoutInflater, R.layout.weather_row, parent, false)
50+
51+
return WeatherViewHolder(binding)
52+
}
53+
54+
override fun getItemCount(): Int = data.size
55+
}
56+
57+
class WeatherViewHolder(val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
58+
fun bind(data: Any) {
59+
binding.setVariable(BR.data, data)
60+
binding.executePendingBindings()
61+
}
62+
}
63+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.example.kotlin.databinding
2+
3+
import android.databinding.BaseObservable
4+
5+
class WeatherData(
6+
var location: String,
7+
var temperature: String,
8+
var info: String,
9+
var imageUrl: String
10+
) : BaseObservable()

0 commit comments

Comments
 (0)