Skip to content

Commit cdb3985

Browse files
author
wing
committed
binding adapter
1 parent 92d9dbf commit cdb3985

File tree

11 files changed

+159
-21
lines changed

11 files changed

+159
-21
lines changed

app/build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ android {
1414
versionName "1.0"
1515
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1616
}
17+
18+
dataBinding {
19+
enabled true
20+
}
1721
buildTypes {
1822
release {
1923
minifyEnabled false
@@ -54,8 +58,12 @@ dependencies {
5458
provided dependencies.javaxAnnotation
5559

5660
compile dependencies.kotlinStdlib
57-
61+
compile dependencies.kotlinReflect
62+
kapt 'com.android.databinding:compiler:1.0-rc5'
5863
// Kotlin
5964
// compile dependencies.kotlinStdlib
6065
testCompile 'junit:junit:4.12'
6166
}
67+
kapt {
68+
generateStubs = true
69+
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
android:icon="@mipmap/ic_launcher"
99
android:label="@string/app_name"
1010
android:supportsRtl="true"
11-
android:theme="@style/AppTheme">
11+
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
1212
<activity android:name=".ui.activity.MainActivity">
1313
<intent-filter>
1414
<action android:name="android.intent.action.MAIN" />

app/src/main/java/com/wingsofts/gankclient/api/GankApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import rx.Observable
99
* Created by wing on 11/23/16.
1010
*/
1111
interface GankApi{
12-
@GET("data/Android/10/1")
12+
@GET("random/data/Android/20")
1313
fun getData():Observable<JsonResult<List<FuckFood>>>
1414
}

app/src/main/java/com/wingsofts/gankclient/bean/FuckFood.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.wingsofts.gankclient.bean
22

3+
import java.util.*
4+
35
/**
46
* Created by wing on 11/23/16.
57
*/
68
data class FuckFood(
79
val _id: String,
810
val createdAt: String,
911
val desc: String,
10-
val images: List<String>,
12+
val images: Array<String>,
1113
val publishedAt: String,
1214
val source: String,
1315
val type: String,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@file:JvmName("BindingAdapter")
2+
3+
package com.wingsofts.gankclient.ui
4+
5+
import android.databinding.BindingAdapter
6+
import android.widget.ImageView
7+
import com.bumptech.glide.Glide
8+
9+
/**
10+
* Created by wing on 11/24/16.
11+
*/
12+
object BindingAdapter {
13+
14+
@BindingAdapter("load_image")
15+
@JvmStatic
16+
fun loadImage(imageView: ImageView, url: String?) = Glide.with(imageView.context).load(url).into(
17+
imageView)
18+
}

app/src/main/java/com/wingsofts/gankclient/ui/activity/MainActivity.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,33 @@ package com.wingsofts.gankclient.ui.activity
22

33
import android.support.v7.app.AppCompatActivity
44
import android.os.Bundle
5+
import android.support.v7.widget.LinearLayoutManager
56
import android.util.Log
67
import android.widget.Toast
78
import com.wingsofts.gankclient.App
89
import com.wingsofts.gankclient.R
910
import com.wingsofts.gankclient.createApi
11+
import com.wingsofts.gankclient.ui.adapter.RandomAdapter
12+
import kotlinx.android.synthetic.main.activity_main.*
1013
import okhttp3.OkHttpClient
1114
import rx.android.schedulers.AndroidSchedulers
1215

1316
class MainActivity : AppCompatActivity() {
1417

15-
override fun onCreate(savedInstanceState: Bundle?) {
16-
super.onCreate(savedInstanceState)
17-
setContentView(R.layout.activity_main)
18+
override fun onCreate(savedInstanceState: Bundle?) {
19+
super.onCreate(savedInstanceState)
20+
setContentView(R.layout.activity_main)
21+
btn_fuck.setOnClickListener {
22+
createApi().getData().observeOn(AndroidSchedulers.mainThread())
23+
.subscribe(
24+
{ res ->
25+
recyclerView.adapter = RandomAdapter(res.results)
26+
recyclerView.layoutManager = LinearLayoutManager(this)
27+
},
28+
{ e -> Log.e("wing", e.message) }
29+
)
30+
}
1831

19-
createApi().getData().observeOn(AndroidSchedulers.mainThread())
20-
.subscribe(
21-
{ Toast.makeText(this,"成功",Toast.LENGTH_SHORT).show()},
22-
{e-> Log.e("wing",e.message)}
23-
)
2432

25-
}
33+
}
2634
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.wingsofts.gankclient.ui.adapter
2+
3+
import android.databinding.ViewDataBinding
4+
import android.support.v7.widget.RecyclerView
5+
import android.view.View
6+
7+
/**
8+
* Created by wing on 11/23/16.
9+
*/
10+
class DataBoundViewHolder<T : ViewDataBinding>(val binding:T) : RecyclerView.ViewHolder(binding.root) {
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.wingsofts.gankclient.ui.adapter
2+
3+
import android.support.v7.widget.RecyclerView
4+
import android.view.LayoutInflater
5+
import android.view.ViewGroup
6+
import com.wingsofts.gankclient.bean.FuckFood
7+
import com.wingsofts.gankclient.databinding.ActivityMainBinding
8+
import com.wingsofts.gankclient.databinding.ItemRandomBinding
9+
10+
/**
11+
* Created by wing on 11/23/16.
12+
*/
13+
class RandomAdapter(private val mList: List<FuckFood>) : RecyclerView.Adapter<DataBoundViewHolder<ItemRandomBinding>>() {
14+
override fun getItemCount(): Int {
15+
return mList.size
16+
}
17+
18+
override fun onBindViewHolder(holder: DataBoundViewHolder<ItemRandomBinding>, position: Int) {
19+
holder.binding.fuckfood = mList[position]
20+
}
21+
22+
override fun onCreateViewHolder(parent: ViewGroup,
23+
viewType: Int): DataBoundViewHolder<ItemRandomBinding> {
24+
return DataBoundViewHolder(
25+
ItemRandomBinding.inflate(LayoutInflater.from(parent.context), parent, false))
26+
}
27+
}
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<layout>
3+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
34
xmlns:tools="http://schemas.android.com/tools"
45
android:id="@+id/activity_main"
56
android:layout_width="match_parent"
67
android:layout_height="match_parent"
7-
android:paddingBottom="@dimen/activity_vertical_margin"
8-
android:paddingLeft="@dimen/activity_horizontal_margin"
9-
android:paddingRight="@dimen/activity_horizontal_margin"
10-
android:paddingTop="@dimen/activity_vertical_margin"
8+
android:orientation="vertical"
119
tools:context="com.wingsofts.gankclient.ui.activity.MainActivity">
1210

13-
<TextView
11+
<Button
12+
android:id="@+id/btn_fuck"
1413
android:layout_width="wrap_content"
1514
android:layout_height="wrap_content"
16-
android:text="Hello World!" />
17-
</RelativeLayout>
15+
android:text="手气不错" />
16+
17+
<android.support.v7.widget.RecyclerView
18+
android:background="#f1f1f1"
19+
android:id="@+id/recyclerView"
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent"
22+
>
23+
24+
</android.support.v7.widget.RecyclerView>
25+
</LinearLayout>
26+
</layout>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
3+
<data>
4+
<variable
5+
name="fuckfood"
6+
type="com.wingsofts.gankclient.bean.FuckFood"/>
7+
</data>
8+
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
9+
android:layout_marginTop="10dp"
10+
android:background="#fff"
11+
android:paddingTop="10dp"
12+
android:layout_width="match_parent"
13+
android:layout_height="wrap_content"
14+
android:orientation="vertical"
15+
>
16+
<LinearLayout
17+
android:paddingRight="16dp"
18+
android:paddingLeft="16dp"
19+
android:paddingBottom="10dp"
20+
android:layout_marginTop="10dp"
21+
android:orientation="vertical"
22+
android:layout_width="match_parent"
23+
android:layout_height="wrap_content"
24+
>
25+
26+
<TextView
27+
android:textColor="#000"
28+
android:textSize="16sp"
29+
android:text="@{fuckfood.desc}"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
/>
33+
<ImageView
34+
app:load_image="@{fuckfood.images[0]}"
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
/>
38+
<TextView
39+
android:layout_marginTop="10dp"
40+
android:text = "@{fuckfood.createdAt}"
41+
android:layout_width="wrap_content"
42+
android:layout_height="wrap_content"
43+
/>
44+
<TextView
45+
android:layout_marginTop="10dp"
46+
android:text="@{fuckfood.who}"
47+
android:layout_width="wrap_content"
48+
android:layout_height="wrap_content"
49+
/>
50+
51+
</LinearLayout>
52+
53+
</android.support.v7.widget.CardView>
54+
</layout>

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ buildscript {
99
classpath 'com.android.tools.build:gradle:2.2.2'
1010
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.4"
1111
classpath "org.jetbrains.kotlin:kotlin-android-extensions:1.0.4"
12+
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
1213
// NOTE: Do not place your application dependencies here; they belong
1314
// in the individual module build.gradle files
1415
}

0 commit comments

Comments
 (0)