Git Product home page Git Product logo

livedatabusx's Introduction

LiveDataBusX的使用

(1)基础配置

在项目的buld.gradle中添加如下代码(jcenter中没有提交审核,所以需要添加这句,之后审核通过,这句话可以去掉)

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url 'https://dl.bintray.com/codefarmerguan/LiveDataBusX'
        }
    }
}

在module项目中进行依赖:

implementation 'com.gzc:livedatabusx:1.0.0'
annotationProcessor 'com.gzc:livedatabusx-processor:1.0.0'

(2)初始化操作

在项目Application的onCreate方法中进行如下操作

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        LiveDataBusX.getInstance().init();
    }
}

(3)事件监听

在需要接收事件的类中进行observe,和EventBus的register方法一样,不过不需要解注册,如下

//无动态key
LiveDataBusX.getInstance().observe(this);
//有动态key
LiveDataBusX.getInstance().observe(this,dynamicKey);

observe方法有两种:一种是无动态key,一种是有动态key。在某些业务场景下,key值可能会随某些属性而变化。

之后在类中写如下的方法来接收事件变化

@Observe(threadMode = ThreadMode.MAIN,sticky = false,append = false,key = "test1")
public void test1(Test1Bean test1Bean){

}

Observe注解有四个属性:

threadMode:指定线程,目前有MAIN,BACKGROUND,ASYNC三个值,和EventBus中的一样、

sticky:是否具有粘性,和EventBus中的粘性相似。究其原理,其实和粘性没有任何关系,执行现象和EventBus的粘性相似,我这里为了好解释,也就使用了sticky这个值

key:静态key

append:静态key与动态key是否拼接。如果没有设置动态key,这里一定要设置false,否则会出现问题

如果设置了动态key,并且append为true,最后的key形态为 静态key::动态key

如果append为false,那么最后的key就是静态key本身。使用者不需要关心这里的形态问题,这里只是做了解释

(4)事件发送

LiveDataBusX.getInstance()
        .post("test1",new Test1Bean());

LiveDataBusX.getInstance()
        .post("test1","动态key",new Test1Bean());

post也有两个方法,与observe的两个方法相对应。

如果没有动态key的情况,使用两参数的方法**:第一个参数为静态key,第二个参数为事件对象**

如果有静态key的情况,使用三参数的方法:第一个参数为静态key,第二个参数为动态key,第三个参数为事件对象

livedatabusx's People

Contributors

stronger-gzc avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.