我正在使用Green Robot的EventBus进行内部事件消息传递,我想使用EventBus在RecyclerView中显示数据。问题是正确接收了EventBus帖子,并在Android 5'''设备,但相同的代码不适用于7''和10''片段。当片段第一次加载时,它适用于所有设备,但是当我尝试使用EventBus绑定数据时,它无法正常工作。
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(colCount, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
SpacesItemDecoration decoration = new SpacesItemDecoration(4);
recyclerView.addItemDecoration(decoration);
recyclerView.setHasFixedSize(true);
CategoryAdapter categoryAdapter = new CategoryAdapter(new ArrayList<Category>(), mListener);
recyclerView.setAdapter(categoryAdapter);
上面的代码块在我的onCreateView方法中。
@Subscribe(threadMode = ThreadMode.MAIN)
public void onClassificationEvent(final EventMessage event) {
Log.e("onClassificationEvent", "onClassificationEvent AllFragment :" + event.getEventId());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (event.getEventId() == "20") {
//All
bindCategories(3);
} else if (event.getEventId() == "21") {
//Veg
bindCategories(1);
} else if (event.getEventId() == "22") {
//NonVeg
bindCategories(2);
}
}
});
}
上面的代码是我的EventBus订阅方法
private void bindCategories(final int classification) {
List<Category> categories = new ArrayList<>();
if (classification == 1) {
categories = DbAccessHelper.CategoriesHelper.getVegCategories(getActivity());
} else if (classification == 2) {
categories = DbAccessHelper.CategoriesHelper.getNonVegCategories(getActivity());
} else if (classification == 3) {
categories = DbAccessHelper.CategoriesHelper.getAllCategories(getActivity());
}
if (categories != null) {
if (!categories.isEmpty()) {
tvError.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
CategoryAdapter categoryAdapter = new CategoryAdapter(categories, mListener);
recyclerView.setAdapter(categoryAdapter);
categoryAdapter.notifyDataSetChanged();
} else {
tvError.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
CategoryAdapter categoryAdapter = new CategoryAdapter(new ArrayList<Category>(), mListener);
recyclerView.setAdapter(categoryAdapter);
}
} else {
tvError.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
CategoryAdapter categoryAdapter = new CategoryAdapter(new ArrayList<Category>(), mListener);
recyclerView.setAdapter(categoryAdapter);
}
}
此方法用于将RecyclerView与数据绑定。有人可以帮我找到问题吗?