gpt4 book ai didi

java - 如何重新创建或刷新 fragment

转载 作者:行者123 更新时间:2023-12-01 11:41:10 25 4
gpt4 key购买 nike

我有用于显示 NavigationDrawer 的 fragment 。其中我有第一部分的标题,其中包含一些信息。此信息发生变化,并且使用 EventBus 我控制此事件,但如何更新此 fragment ?

只有当我旋转设备时,下面的实现才有效。

从 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();

}
};

最佳答案

我可以看到您正在将 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适配器上的方法,以便可以在屏幕上更新新值。

关于java - 如何重新创建或刷新 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com