Created
December 25, 2019 07:31
-
-
Save Hasiy/97441ba9fb355c27f9cd5094af48ce66 to your computer and use it in GitHub Desktop.
数据通信总线LiveDataBus
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
public final class LiveDataBus { | |
private final Map<String, MutableLiveData<Object>> bus; | |
private LiveDataBus() { | |
bus = new HashMap<>(); | |
} | |
private static class SingletonHolder { | |
private static final LiveDataBus DATA_BUS = new LiveDataBus(); | |
} | |
public static LiveDataBus get() { | |
return SingletonHolder.DATA_BUS; | |
} | |
public <T> MutableLiveData<T> getChannel(String target, Class<T> type) { | |
if (!bus.containsKey(target)) { | |
bus.put(target, new MutableLiveData<>()); | |
} | |
return (MutableLiveData<T>) bus.get(target); | |
} | |
public MutableLiveData<Object> getChannel(String target) { | |
return getChannel(target, Object.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
注册订阅:
发送消息:
LiveDataBus.get().getChannel("key_test").setValue(true);