Search in sources :

Example 1 with User

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

the class ConstactFragment method initListView.

private void initListView() {
    mAdapter = new ArrayAdapter<User>(getActivity(), android.R.layout.simple_list_item_1, mConstacts);
    mListView.setAdapter(mAdapter);
    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // 点击用户,发布消息,
            // tag为MenuFragment.CLICK_TAG,接收函数在MenuFragment的updateClickUserName中
            EventBus.getDefault().post(mConstacts.get(position), MenuFragment.CLICK_TAG);
        }
    });
}
Also used : User(org.simple.eventbus.demo.bean.User) OnItemClickListener(android.widget.AdapterView.OnItemClickListener) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView)

Example 2 with User

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

the class MenuFragment method postListData.

/**
     * 发布参数是List<T>类型的事件, 接收函数为@{@see #subcribeList(List)}
     */
private void postListData() {
    List<User> userLisr = new ArrayList<User>();
    for (int i = 0; i < 5; i++) {
        userLisr.add(new User("user - " + i));
    }
    EventBus.getDefault().post(userLisr);
}
Also used : StickyUser(org.simple.eventbus.demo.bean.StickyUser) User(org.simple.eventbus.demo.bean.User) ArrayList(java.util.ArrayList)

Example 3 with User

use of org.simple.eventbus.demo.bean.User 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

User (org.simple.eventbus.demo.bean.User)3 View (android.view.View)2 StickyUser (org.simple.eventbus.demo.bean.StickyUser)2 Intent (android.content.Intent)1 OnClickListener (android.view.View.OnClickListener)1 AdapterView (android.widget.AdapterView)1 OnItemClickListener (android.widget.AdapterView.OnItemClickListener)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 StickyActivity (org.simple.eventbus.demo.StickyActivity)1