天天看点

RxBinding的使用

RxBinding是JakeWharton的一个开源库 , 它提供了一套基于RxJava的BindingApI. 可以帮助我们简化控件/视图添加的触发的响应事件,而且使用起来非常简单

点击打开链接

在build.gradle中引用以下文件

compile 'com.jakewharton.rxbinding:rxbinding:1.0.0'

compile 'com.jakewharton.rxbinding:rxbinding-support-v4:1.0.0'

compile 'com.jakewharton.rxbinding:rxbinding-appcompat-v7:1.0.0'

compile 'com.jakewharton.rxbinding:rxbinding-design:1.0.0'

<b>[html]</b> view plain copy

but = (Button) findViewById( R.id.bt) ;

RxView.clicks(but)

//表示在2秒之内只取一个点击事件,防抖操作

.throttleFirst(2, TimeUnit.SECONDS)

.subscribe(new Action1&lt;Void&gt;() {

    @Override

    public void call(Void aVoid) {

        Toast.makeText(mContext, "点击了....", Toast.LENGTH_SHORT).show();

    }

});

RxView.longClicks(but).subscribe(new Action1&lt;Void&gt;() {

        Toast.makeText(mContext, "long click...", Toast.LENGTH_SHORT).show();

cb = (CheckBox) findViewById( R.id.checkbox );

RxCompoundButton.checkedChanges(cb).subscribe(new Action1&lt;Boolean&gt;() {

    public void call(Boolean aBoolean) {

        Toast.makeText(mContext, aBoolean? "被选中":"取消选择", Toast.LENGTH_SHORT).show();

listView = (ListView) findViewById( R.id.listview );

RxAdapterView.itemClicks( listView )

.subscribe(new Action1&lt;Integer&gt;() {

    public void call(Integer integer) {

         Toast.makeText(ListActivity.this, "item click " + integer ,                    Toast.LENGTH_SHORT).show();

}

}) ;

RxAdapterView.itemLongClicks( listView)

 .subscribe(new Action1&lt;Integer&gt;() {

     public void call(Integer integer) {

         Toast.makeText(ListActivity.this, "item long click " + integer ,Toast.LENGTH_SHORT).show();

 }

editText = (EditText) findViewById( R.id.editText );

final ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_expandable_list_item_1 ); listView.setAdapter( adapter );

RxTextView.textChanges( editText )

    .debounce( 600 , TimeUnit.MILLISECONDS )

    .map(new Func1&lt;CharSequence, String&gt;() {

      @Override

      public String call(CharSequence charSequence) {

        String key = charSequence.toString() ;

    return key ;

     }

})

.observeOn( Schedulers.io() )

.map(new Func1&lt;String, List&lt;String&gt;&gt;() {

    public List&lt;String&gt; call(String keyWord ) {

        List&lt;String&gt; dataList = new ArrayList&lt;String&gt;() ;

        if ( ! TextUtils.isEmpty( keyWord )){

            for ( String s : getData() ) {

                if (s != null) {

                     if (s.contains(keyWord)) {

                            dataList.add(s);

                    }

                }

             }

            }

                 return dataList ;

        }

 })

.observeOn( AndroidSchedulers.mainThread() )

.subscribe(new Action1&lt;List&lt;String&gt;&gt;() {

     @Override

     public void call(List&lt;String&gt; strings) {

        adapter.clear();

        adapter.addAll( strings );

        adapter.notifyDataSetChanged();

      }

But3 = (Button) findViewById( R.id.bt3) ;

mVerifyCodeObservable = RxView.clicks(but3)

        .throttleFirst(20, TimeUnit.SECONDS)

        .subscribeOn(AndroidSchedulers.mainThread())

        .doOnNext(new Action1&lt;Void&gt;() {

            @Override

            public void call(Void aVoid) {

                RxView.enabled(but3).call(false);

        });

mVerifyCodeObservable.subscribe(new Action1&lt;Void&gt;() {

        Observable.interval(1,TimeUnit.SECONDS,AndroidSchedulers.mainThread())

                .take(20)

                .subscribe(new Observer&lt;Long&gt;() {

                    @Override

                    public void onCompleted() {

                        RxTextView.text(but3).call("获取验证码");

                        RxView.enabled(but3).call(true);

                    public void onError(Throwable e) {

                    public void onNext(Long aLong) {

                        RxTextView.text(but3).call("剩余" + (20-aLong));

                });

RxBinding把发布--订阅的模式用在了控件的点击和文本的变化上

RxBinding几乎支持工作中常用的所有控件以及事件

RxBinding引用库还有对应的kotlin支持库