OTTO是Square推出的库,地址:https://github.com/square/otto
先来看看otto的官方介绍
An enhanced Guava-based event bus with emphasis on Android support.
Otto is an event bus designed to decouple different parts of your application while still allowing them to communicate efficiently.
Forked from Guava, Otto adds unique functionality to an already refined event bus as well as specializing it to the Android platform.
otto基于Guava项目的Android支持库,如果你在Android程序开发的过程中想要不同的组件之间进行有效的通信可以使用这个库。通过otto库可以
降低程序之间的耦合性。
public class AppBus extends Bus {BusEventData是我们需要传递的数据,是一个普通的Java Bean
private static AppBus bus;
public static AppBus getInstance() {
if (bus == null) {
bus = new AppBus();
}
return bus;
}
}
public class BusEventData {
public String content;
public BusEventData(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
public class TestFragment extends Fragment {
private EditText mContentET;
public static TestFragment getInstance() {
TestFragment fragment = new TestFragment();
// fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test, container, false);
mContentET = (EditText) view.findViewById(R.id.content);
return view;
}
/**
* 接收MyScrollView中onScrollChanged的变化
*/
@Subscribe
public void onMyScrollChange(ScrollEventData data) {
System.out.println("====" + data);
}
@Override
public void onStart() {
super.onStart();
//注册到bus事件总线中
AppBus.getInstance().register(this);
}
@Override
public void onStop() {
super.onStop();
AppBus.getInstance().unregister(this);
}
/**
* 定义订阅者,Activity中发布的消息,在此处会接收到,在此之前需要先在程序中register,看
* 上面的onStart和onStop函数
*/
@Subscribe
public void setContent(BusEventData data) {
mContentET.setText(data.getContent());
}
@Subscribe
public void onDataChange(String sss) {
System.out.println("====" + sss);
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container, TestFragment.getInstance());
transaction.commit();
}
public void doOnClick(View view) {
switch (view.getId()) {
case R.id.btn:
AppBus.getInstance().post(new BusEventData("somebody alive"));
break;
case R.id.btn2:
AppBus.getInstance().post("hello");
break;
default:
break;
}
}
@Override
protected void onPause() {
super.onPause();
//AppBus.getInstance().unregister(this);
}
@Override
protected void onResume() {
super.onResume();
//AppBus.getInstance().register(this);
}
//
//@Produce
//public BusEventData produceFragmentData() {
//return new BusEventData("This data com from activity");
//}
}
public class MyScrollView extends ScrollView {
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context) {
super(context);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
//发布消息,TestFragment中注册了对此事件的监听,它会收到数据
AppBus.getInstance().post(new ScrollEventData(l, t, oldl, oldt));
}
@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX,
boolean clampedY) {
// TODO Auto-generated method stub
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。