Created
April 24, 2016 18:06
-
-
Save SojiroNishimura/b3dea662013a272390712699248a2df2 to your computer and use it in GitHub Desktop.
RxAndroidでダブルクリックイベントをキャプチャする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.callittips.snishimura.rxandroidsample; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import com.jakewharton.rxbinding.view.RxView; | |
import com.jakewharton.rxbinding.widget.RxCompoundButton; | |
import com.jakewharton.rxbinding.widget.RxTextView; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import butterknife.Bind; | |
import butterknife.ButterKnife; | |
import rx.Observable; | |
import rx.Observer; | |
import rx.Subscriber; | |
import rx.Subscription; | |
import rx.android.schedulers.AndroidSchedulers; | |
import rx.functions.Action1; | |
import rx.functions.Func1; | |
import rx.functions.Func2; | |
import rx.subscriptions.CompositeSubscription; | |
public class MainActivity extends AppCompatActivity { | |
private final CompositeSubscription mSubscriptions = new CompositeSubscription(); | |
@Bind(R.id.button) | |
Button mButton; | |
@Bind(R.id.label) | |
TextView mLabel; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ButterKnife.bind(this); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
// クリックイベントのストリームを作成 | |
Observable<Void> doubleClickObservable = RxView.clicks(mButton); | |
Subscription s = doubleClickObservable.map(new Func1<Void, Integer>() { | |
@Override | |
public Integer call(Void aVoid) { | |
// クリックされたら1を返すように設定 | |
return 1; | |
} | |
}) | |
.buffer(1, TimeUnit.SECONDS) // イベントをまとめる | |
.map(new Func1<List<Integer>, Integer>() { | |
@Override | |
public Integer call(List<Integer> integers) { | |
// イベント発生回数をfilterに送る | |
Log.d("AAA", integers.size() + "times clicked in a second"); | |
return integers.size(); | |
} | |
}) | |
.filter(new Func1<Integer, Boolean>() { | |
@Override | |
public Boolean call(Integer clicked) { | |
// 1秒間のイベント発生回数が2回の場合のみ抽出する(ダブルクリックの検出) | |
boolean isDoubleClicked = clicked == 2; | |
Log.d("AAA", "isDoubleClicked : " + isDoubleClicked); | |
return isDoubleClicked; | |
} | |
}) | |
.map(new Func1<Integer, Integer>() { | |
@Override | |
public Integer call(Integer integer) { | |
// ダブルクリックを検出したら1を返す | |
return 1; | |
} | |
}) | |
.scan(new Func2<Integer, Integer, Integer>() { | |
@Override | |
public Integer call(Integer acc, Integer count) { | |
// これまでの処理結果を累積してObserverへ通知する | |
return acc + count; | |
} | |
}) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Action1<Integer>() { | |
@Override | |
public void call(Integer count) { | |
Log.d("AAA", "Double clicked : count = " + count); | |
mLabel.setText(Integer.toString(count)); | |
} | |
}); | |
// ViewイベントのObservableは暗黙的にViewへの参照を持つため | |
// メモリリークを防ぐために明示的に管理する必要がある。 | |
// SubscriberのonCompletedが実行される場合は自動的にメモリ管理が行われるため | |
// この処理とonPauseのunsubscribeは不要(らしい) | |
mSubscriptions.add(s); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
mSubscriptions.unsubscribe(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment