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);
}
});
}
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);
}
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;
}
Aggregations