Search in sources :

Example 1 with StickyUser

use of org.simple.eventbus.demo.bean.StickyUser in project AndroidEventBus by hehonghui.

the class MenuFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.menu_fragment, container, false);
    mUserNameTv = (TextView) rootView.findViewById(R.id.click_tv);
    mThreadTv = (TextView) rootView.findViewById(R.id.timer_tv);
    // 发布事件
    rootView.findViewById(R.id.my_post_button).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            EventBus.getDefault().post(new User("Mr.Simple" + new Random().nextInt(100)));
        }
    });
    // 发布移除事件的按钮
    rootView.findViewById(R.id.my_remove_button).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // 移除用户
            EventBus.getDefault().post(new User("User - 1"), REMOVE_TAG);
        }
    });
    // 发布异步事件的按钮
    rootView.findViewById(R.id.my_post_async_event_button).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // 将目标函数执行在异步线程中
            EventBus.getDefault().post(new User("async-user"), ASYNC_TAG);
        }
    });
    // 发布事件,传递的是List数据
    rootView.findViewById(R.id.my_post_list_btn).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            postListData();
        }
    });
    // 发布事件,调用的是父类中的函数
    rootView.findViewById(R.id.my_post_to_supper_btn).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            postEventToSuper();
        }
    });
    // 发布事件,将事件投递到子线程中
    rootView.findViewById(R.id.my_post_to_thread_btn).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // post 给PostThread线程
            EventBus.getDefault().post("I am MainThread", THREAD_TAG);
        }
    });
    // 发布事件,事件类型为原始类型,比如int, boolean, float等
    rootView.findViewById(R.id.post_primitive_btn).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            EventBus.getDefault().post(12345);
            // 整型数组
            EventBus.getDefault().post(new int[] { 12, 24 });
            EventBus.getDefault().post(true);
        }
    });
    startThreads();
    EventBus.getDefault().register(this);
    rootView.findViewById(R.id.post_sticky_tv).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // 发布Sticky事件
            EventBus.getDefault().postSticky(new StickyUser("我来自Sticky事件 - StickyUser类"));
            // 跳转页面
            Intent intent = new Intent(getActivity(), StickyActivity.class);
            startActivity(intent);
        }
    });
    return rootView;
}
Also used : StickyUser(org.simple.eventbus.demo.bean.StickyUser) User(org.simple.eventbus.demo.bean.User) Random(java.util.Random) OnClickListener(android.view.View.OnClickListener) StickyUser(org.simple.eventbus.demo.bean.StickyUser) Intent(android.content.Intent) TextView(android.widget.TextView) View(android.view.View) StickyActivity(org.simple.eventbus.demo.StickyActivity)

Aggregations

Intent (android.content.Intent)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 TextView (android.widget.TextView)1 Random (java.util.Random)1 StickyActivity (org.simple.eventbus.demo.StickyActivity)1 StickyUser (org.simple.eventbus.demo.bean.StickyUser)1 User (org.simple.eventbus.demo.bean.User)1