Skip to content

Commit 375d52a

Browse files
committed
add sample of RxBinding-RxView for Java and Kotlin
1 parent 0a069f9 commit 375d52a

File tree

9 files changed

+191
-16
lines changed

9 files changed

+191
-16
lines changed

app/build.gradle

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
plugins {
2+
id 'io.franzbecker.gradle-lombok' version '1.14'
3+
}
4+
15
apply plugin: 'com.android.application'
26

37
android {
@@ -22,7 +26,28 @@ dependencies {
2226
implementation fileTree(dir: 'libs', include: ['*.jar'])
2327
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
2428
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
29+
30+
// RxJava
31+
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
32+
// Because RxAndroid releases are few and far between, it is recommended you also
33+
// explicitly depend on RxJava's latest version for bug fixes and new features.
34+
// (see https://github.com/ReactiveX/RxJava/releases for latest 2.x.x version)
35+
implementation 'io.reactivex.rxjava2:rxjava:2.1.16'
36+
implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
37+
38+
// Retrofit
39+
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
40+
implementation group: 'com.squareup.retrofit2', name: 'converter-gson', version: '2.4.0'
41+
implementation group: 'com.squareup.retrofit2', name: 'adapter-rxjava2', version: '2.4.0'
42+
43+
// Kotlin
44+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
45+
implementation "org.jetbrains.anko:anko:$anko_version"
46+
2547
testImplementation 'junit:junit:4.12'
2648
androidTestImplementation 'com.android.support.test:runner:1.0.2'
2749
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
2850
}
51+
52+
apply plugin: 'kotlin-android'
53+
apply plugin: 'kotlin-android-extensions'

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<category android:name="android.intent.category.LAUNCHER" />
1717
</intent-filter>
1818
</activity>
19+
<activity android:name=".activity.RxViewActivity" />
20+
<activity android:name=".activity.RxViewKtActivity"></activity>
1921
</application>
2022

2123
</manifest>
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
package com.buzzbil.rxandroidsample;
22

3-
import android.support.v7.app.AppCompatActivity;
3+
import android.content.Intent;
44
import android.os.Bundle;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.view.View;
7+
import android.widget.Button;
8+
import android.widget.LinearLayout;
9+
10+
import com.buzzbil.rxandroidsample.activity.RxViewActivity;
11+
import com.buzzbil.rxandroidsample.activity.RxViewKtActivity;
512

613
public class MainActivity extends AppCompatActivity {
714

815
@Override
916
protected void onCreate(Bundle savedInstanceState) {
1017
super.onCreate(savedInstanceState);
18+
1119
setContentView(R.layout.activity_main);
1220
}
21+
22+
public void startRxView(View view){
23+
startActivity(new Intent(this, RxViewActivity.class));
24+
}
25+
26+
public void startRxViewKt(View view){
27+
startActivity(new Intent(this, RxViewKtActivity.class));
28+
}
29+
1330
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.buzzbil.rxandroidsample.activity;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.widget.Button;
6+
import android.widget.CheckBox;
7+
import android.widget.Toast;
8+
9+
import com.buzzbil.rxandroidsample.R;
10+
import com.jakewharton.rxbinding2.view.RxView;
11+
import com.jakewharton.rxbinding2.widget.RxCompoundButton;
12+
13+
import io.reactivex.Observable;
14+
import io.reactivex.disposables.Disposable;
15+
import io.reactivex.functions.BiFunction;
16+
import io.reactivex.functions.Consumer;
17+
import io.reactivex.functions.Predicate;
18+
19+
public class RxViewActivity extends AppCompatActivity {
20+
21+
@Override
22+
protected void onCreate(Bundle savedInstanceState) {
23+
super.onCreate(savedInstanceState);
24+
setContentView(R.layout.activity_rx_view);
25+
26+
Button button = findViewById(R.id.rx_view_btn);
27+
Observable<Object> clickObs = RxView.clicks(button);
28+
29+
30+
CheckBox checkBox = findViewById(R.id.rx_view_checkbox);
31+
Observable<Boolean> checkedTrueObs = RxCompoundButton.checkedChanges(checkBox)
32+
.skipInitialValue();
33+
34+
Disposable subscribe = clickObs
35+
// 클릭 스트림에 체크 스트림의 마지막 값을 조합해서 새로운 스트림을 만든다.
36+
.withLatestFrom(checkedTrueObs, new BiFunction<Object, Boolean, Boolean>() {
37+
@Override
38+
public Boolean apply(Object o, Boolean checked) throws Exception {
39+
// 새로운 스트림의 데이터는 체크 상태인지 아닌지를 나타낸다.
40+
return checked;
41+
}
42+
})
43+
// 체크 상태인 경우만 필터링 한다.
44+
.filter(new Predicate<Boolean>() {
45+
@Override
46+
public boolean test(Boolean aBoolean) throws Exception {
47+
return aBoolean == true;
48+
}
49+
})
50+
// 여기까지 오면 클릭 시점에 마지막 체크 상태가 true 인 경우인 스트림을 나타낸다.
51+
.subscribe(new Consumer<Object>() {
52+
@Override
53+
public void accept(Object o) throws Exception {
54+
Toast.makeText(RxViewActivity.this, "result : " + o, Toast.LENGTH_SHORT).show();
55+
}
56+
});
57+
}
58+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.buzzbil.rxandroidsample.activity
2+
3+
import android.os.Bundle
4+
import android.support.v7.app.AppCompatActivity
5+
import com.buzzbil.rxandroidsample.R
6+
import com.jakewharton.rxbinding2.view.RxView
7+
import com.jakewharton.rxbinding2.widget.RxCompoundButton
8+
import io.reactivex.functions.BiFunction
9+
import org.jetbrains.anko.find
10+
import org.jetbrains.anko.toast
11+
12+
class RxViewKtActivity : AppCompatActivity() {
13+
14+
15+
override fun onCreate(savedInstanceState: Bundle?) {
16+
super.onCreate(savedInstanceState)
17+
setContentView(R.layout.activity_rx_view)
18+
19+
val clicks = RxView.clicks(find(R.id.rx_view_btn))
20+
val checkedChanges = RxCompoundButton.checkedChanges(find(R.id.rx_view_checkbox))
21+
22+
clicks
23+
.withLatestFrom(
24+
checkedChanges,
25+
BiFunction<Any, Boolean, Boolean> { _, checked -> checked }
26+
)
27+
.filter { it }
28+
.subscribe { toast("result : $it") }
29+
}
30+
}
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
xmlns:tools="http://schemas.android.com/tools"
2+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
53
android:layout_width="match_parent"
64
android:layout_height="match_parent"
7-
tools:context=".MainActivity">
5+
android:padding="10dp">
86

9-
<TextView
10-
android:layout_width="wrap_content"
11-
android:layout_height="wrap_content"
12-
android:text="Hello World!"
13-
app:layout_constraintBottom_toBottomOf="parent"
14-
app:layout_constraintLeft_toLeftOf="parent"
15-
app:layout_constraintRight_toRightOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
7+
<LinearLayout
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent"
10+
android:orientation="vertical">
11+
<Button
12+
android:layout_width="match_parent"
13+
android:layout_height="wrap_content"
14+
android:onClick="startRxView"
15+
android:text="RxView"/>
16+
<Button
17+
android:layout_width="wrap_content"
18+
android:layout_height="wrap_content"
19+
android:onClick="startRxViewKt"
20+
android:text="RxView Kt"/>
21+
</LinearLayout>
1722

18-
</android.support.constraint.ConstraintLayout>
23+
</ScrollView>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
tools:context=".activity.RxViewActivity">
9+
10+
<Button
11+
android:id="@+id/rx_view_btn"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:text="RxButton"/>
15+
16+
<CheckBox
17+
android:id="@+id/rx_view_checkbox"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content"
20+
android:text="Enable!!"/>
21+
22+
</LinearLayout>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".activity.RxViewKtActivity">
8+
9+
<Button
10+
android:id="@+id/rx_view_btn_kt"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content" />
13+
</android.support.constraint.ConstraintLayout>

build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

3-
buildscript {
3+
buildscript {
4+
ext.kotlin_version = '1.2.30'
5+
ext.anko_version = '0.10.5'
6+
47

58
repositories {
69
google()
@@ -12,7 +15,7 @@ buildscript {
1215

1316
// NOTE: Do not place your application dependencies here; they belong
1417
// in the individual module build.gradle files
15-
}
18+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
1619
}
1720

1821
allprojects {

0 commit comments

Comments
 (0)