compile 'org.greenrobot:eventbus:3.0.0'
我的frament订户:
public class FragmentSort extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
EventBus.getDefault().register(this);
View rootView = inflater.inflate(R.layout.fragment, container, false);
return rootView;
}
@Subscribe
public void onChangeFilter(Integer categoryId) {
// NOT CALL
}
这是我发送消息的主要内容:
public class MainFragment extends Fragment {
private void searchViewPostAction(Integer categoryId) {
searchView.clearFocus();
EventBus.getDefault().post(new CategoryFilterEvent(categoryId));
}
}
这是我的POJO:
public class CategoryFilterEvent {
private Integer categoryId;
public CategoryFilterEvent(Integer categoryId) {
this.categoryId = categoryId;
}
}
方法MainFragment.searchViewPostAction()
是呼叫,但是消息未收到。
方法FragmentSort.onChangeFilter()
不致电。
为什么?
答案 0 :(得分:1)
为CategoryFilterEvent
public int getCategoryId() {
return categoryId;
}
并改变:
@Subscribe
public void onChangeFilter(Integer categoryId) {
// NOT CALL
}
为:
@Subscribe
public void onChangeFilter(CategoryFilterEvent event) {
// THIS WILL BE CALLED
int categoryId = event.getCategoryId();
}