RxAndroidEventsSample, BusEvents实现基础 rxjava/RxAndroid

分享于 

6分钟阅读

GitHub

  繁體 雙語
BusEvents implementation base RxJava/RxAndroid
  • 源代码名称:RxAndroidEventsSample
  • 源代码网址:http://www.github.com/wangjiegulu/RxAndroidEventsSample
  • RxAndroidEventsSample源代码文档
  • RxAndroidEventsSample源代码下载
  • Git URL:
    git://www.github.com/wangjiegulu/RxAndroidEventsSample.git
    Git Clone代码到本地:
    git clone http://www.github.com/wangjiegulu/RxAndroidEventsSample
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/wangjiegulu/RxAndroidEventsSample
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    RxAndroidEventsSample

    BusEvents实现基础 RxJava/RxAndroid/AndroidInject.


    http://www.cnblogs.com/tiantianbyconan/p/4578699.html


    AndroidBucket:My基础库
    AndroidInject:My注入库
    RxJava
    RxAndroid
    retrolambda

    如何使用注释( 推荐)

    It's realy simple to use with annotation

    1必须是配置呈现以支持注释( 例如: 用于在 AndroidInject 库中扩展AIAppCompatActivity的BaseActivity

    privateRxBusAnnotationManager rxBusAnnotationManager;@Overridepublicvoid parserMethodAnnotations(Method method) throws Exception {
     if (method.isAnnotationPresent(Accept.class)) {
     if (null== rxBusAnnotationManager) {
     rxBusAnnotationManager =newRxBusAnnotationManager(this);
     }
     rxBusAnnotationManager.parserObservableEventAnnotations(method);
     }
    }@Overrideprotectedvoid onDestroy() {
     super.onDestroy();
     if (null!= rxBusAnnotationManager) {
     rxBusAnnotationManager.clear();
     }
    }

    2.在你的当前( 例如。 你的Activity ),并使用 @Accept 注释

    @Acceptpublicvoid onPostAccept(Object tag, AddFeedsEvent event) {
     // todo: Accept event and process here (in main thread)}

    或者

    @Accept(
     acceptScheduler=AcceptScheduler.NEW_THREAD,
     value= {
     @AcceptType(tag=ActionEvent.CLOSE, clazz=String.class),
     @AcceptType(tag=ActionEvent.BACK, clazz=String.class),
     @AcceptType(tag=ActionEvent.EDIT, clazz=String.class),
     @AcceptType(tag=ActionEvent.REFRESH, clazz=String.class)
     }
     )
     publicvoid onPostAccept(Object tag, Object actionEvent) {
     Logger.d(TAG, "[ActionEvent]onPostAccept action event name: "+ actionEvent);
     // todo: Accept event and process here (in new thread) }

    @Accept:

    
    acceptScheduler: Perform this method on a specified scheduler;
    
    
    value[]: AcceptType annotation array that specify the types this method can be accept. 
    
    
    
    

    @AcceptType:

    
    tag: The tag of the event.
    
    
    clazz: The Class of the event.
    
    
    
    

    3在以下位置发布事件:

    AddFeedsEvent addFeedsEvent =newAddFeedsEvent();// Construct a AddFeedsEvent Object...RxBus.get().post(addFeedsEvent);

    4指定执行程序或者处理程序( 可选)

    publicclassMyApplicationextendsApplication {
     privateExecutor acceptExecutor =Executors.newCachedThreadPool();
     privateHandler handler =newHandler(Looper.getMainLooper());
     @OverridepublicvoidonCreate() {
     super.onCreate();
     DefaultAcceptConfiguration.getInstance().registerAcceptConfiguration(newDefaultAcceptConfiguration.OnDefaultAcceptConfiguration() {
     @OverridepublicExecutorapplyAcceptExecutor() {
     return acceptExecutor;
     }
     @OverridepublicHandlerapplyAcceptHandler() {
     return handler;
     }
     });
     }
    }




    如何使用注释( 不建议使用)?

    register-一个观察者

    Observable<String> addOb =RxBus.get()
    . register("addFeedTag", String.class);
    addOb.observeOn(AndroidSchedulers.mainThread())
    . subscribe(s -> {
     // todo: Accept event and process here });

    在任何地方发布一个事件

    RxBus.get().post("addFeedTag", "hello RxBus!");

    取消注册观察者

    RxBus.get().unregister("addFeedTag", addOb);
    许可证
    
    Copyright 2015 Wang Jie
    
    
    
    Licensed under the Apache License, Version 2.0 (the"License");
    
    
    you may not use this file except in compliance with the License.
    
    
    You may obtain a copy of the License at
    
    
    
     http://www.apache.org/licenses/LICENSE-2.0
    
    
    
    Unless required by applicable law or agreed to in writing, software
    
    
    distributed under the License is distributed on an"AS IS" BASIS,
    
    
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    
    
    See the License for the specific language governing permissions and
    
    
    limitations under the License.
    
    
    
    

    相关文章