RxBinding 的简单使用

一、git地址

https://github.com/JakeWharton/RxBinding

二、androidStudio 使用

一般的包下面就用

compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'

v4'support-v4' library bindings:

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

'appcompat-v7' library bindings:

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

'design' library bindings:

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

三、代码示例

  • Button 防抖处理

     button = (Button) findViewById( R.id.bt ) ;
    
         RxView.clicks( button )
                 .throttleFirst( 2 , TimeUnit.SECONDS )   //两秒钟之内只取一个点击事件,防抖操作
                 .subscribe(new Action1() {
                     @Override
                     public void call(Void aVoid) {
                         Toast.makeText(MainActivity.this, "点击了", Toast.LENGTH_SHORT).show();
                     }
                 }) ;
    
  • 按钮的长按时间监听

     button = (Button) findViewById( R.id.bt ) ;
    
     //监听长按时间
     RxView.longClicks( button)
          .subscribe(new Action1() {
              @Override
             public void call(Void aVoid) {
             Toast.makeText(MainActivity.this, "long click  !!", Toast.LENGTH_SHORT).show();
             }
         }) ;
    
  • listView 的点击事件、长按事件处理

    listView = (ListView) findViewById( R.id.listview );
    
     List list = new ArrayList<>() ;
         for ( int i = 0 ; i < 40 ; i++ ){
             list.add( "sss" + i ) ;
         }
    
     final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1 );
         adapter.addAll( list );
    
     listView.setAdapter( adapter );
    
     //item click event
     RxAdapterView.itemClicks( listView )
         .subscribe(new Action1() {
             @Override
             public void call(Integer integer) {
             Toast.makeText(ListActivity.this, "item click " + integer , Toast.LENGTH_SHORT).show();
                 }
             }) ;
    
     //item long click
     RxAdapterView.itemLongClicks( listView)
         .subscribe(new Action1() {
             @Override
             public void call(Integer integer) {
                 Toast.makeText(ListActivity.this, "item long click " + integer , Toast.LENGTH_SHORT).show();
                 }
             }) ;
    
- 用户登录界面,勾选同意隐私协议,登录按钮就变高亮
button = (Button) findViewById( R.id.login_bt );
checkBox = (CheckBox) findViewById( R.id.checkbox );

RxCompoundButton.checkedChanges( checkBox )
    .subscribe(new Action1() {
        @Override
        public void call(Boolean aBoolean) {
            button.setEnabled( aBoolean );
            button.setBackgroundResource( aBoolean ? R.color.button_yes : R.color.button_no );
            }
        }) ;
```

效果图

  • 搜索的时候,关键词联想功能 。debounce()在一定的时间内没有操作就会发送事件。

     editText = (EditText) findViewById( R.id.editText );
     listView = (ListView) findViewById( R.id.listview );
    
     final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1 );
     listView.setAdapter( adapter );
    
     RxTextView.textChanges( editText )
                 .debounce( 600 , TimeUnit.MILLISECONDS )
                 .map(new Func1() {
                     @Override
                     public String call(CharSequence charSequence) {
                         //get the keyword
                         String key = charSequence.toString() ;
                         return key ;
                     }
                 })
                 .observeOn( Schedulers.io() )
                 .map(new Func1>() {
                     @Override
                     public List call(String keyWord ) {
                         //get list
                         List dataList = new ArrayList() ;
                         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>() {
                     @Override
                     public void call(List strings) {
                         adapter.clear();
                         adapter.addAll( strings );
                         adapter.notifyDataSetChanged();
                     }
                 }) ;
    

    运行效果

你可能感兴趣的:(RxBinding)