EventBus:@Subscribe - 没有收到消息

时间:2017-10-26 17:38:07

标签: android greenrobot-eventbus

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() 致电。

为什么?

1 个答案:

答案 0 :(得分:1)

CategoryFilterEvent

添加一个getter方法
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();
}