我有片段用于显示NavigationDrawer。在那里我有第一节标题与一些信息。此信息更改,并且EventBus i控制此事件,但如何更新此片段?
以下实现仅在我旋转设备时才有效。
从MainActivity中提取:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
mNavigationNavDrawerFragment = (Nav_DrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationNavDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
inizialized(mServices);
}
....
....
EventBus.getDefault().postSticky(new PersonDetails(mNomeCognome, mEmail));
//HERE HOW CAN REFRESH OR RECREATE THIS mNavigationNavDrawerFragment?
}
从Nav_DrawerFragment中提取:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
mDrawerList = (RecyclerView) view.findViewById(R.id.drawerList);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mDrawerList.setLayoutManager(layoutManager);
mDrawerList.setHasFixedSize(true);
final List<NavigationItem> navigationItems = getMenu();
//HERE I SET THE UPDATE DATA
Nav_adapter adapter = new Nav_adapter(navigationItems,mNomeCognome,mEmail);
adapter.setNavigationDrawerCallbacks(this);
mDrawerList.setAdapter(adapter);
selectItem(mCurrentSelectedPosition,items);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().registerSticky(this);
}
public void onEventMainThread(PersonDetails event){
mNomeCognome = event.mNomeCognome;
mEmail = event.mEmail;
}
public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) return;
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) return;
getActivity().invalidateOptionsMenu();
}
};
答案 0 :(得分:1)
我可以看到您正在使用带适配器的RecyclerView
Nav_adapter adapter = new Nav_adapter(navigationItems,mNomeCognome,mEmail);
然后您将更新用于在事件
上创建适配器的值mNomeCognome = event.mNomeCognome;
mEmail = event.mEmail;
问题是你没有将这些值传递给适配器。
因此,您必须创建一种方法将这些更新的值传递给现有的适配器,如下所示:
// first define your adapter as a field:
Nav_adapter adapter;
// then on your event
public void onEventMainThread(PersonDetails event){
mNomeCognome = event.mNomeCognome;
mEmail = event.mEmail;
adapter.setNewValues(mNomeCognome, mEmail);
}
然后在此setNewValues
内更新适配器值,不要忘记调用适配器上的notifyItemChanged
或notifyItemRangeChanged
方法之一,以便新值可以在屏幕上更新。