gpt4 book ai didi

java - 带 fragment 的抽屉导航(带 ViewPager)重新打开时消失

转载 作者:行者123 更新时间:2023-12-01 19:23:26 31 4
gpt4 key购买 nike

我试图为我的应用程序创建一个导航,使用抽屉导航我的 fragment 之一(HomeFragment)有一个 ViewPager,可容纳 3 个 fragment (Bundy Clock、Announcement、Calender)

当我在启动时运行我的应用程序时,它工作正常,我可以在我的 HomeFragment 的 3 个 fragment (Bundy Clock、公告、日历)之间切换。但是当我在抽屉导航中选择项目(主页)时,HomeFragment 中的 3 个 fragment 消失了,我的 logcat 中没有错误,并且应用程序不会阻止它,只是 ViewPager 中的 3 个 fragment 消失了。

MainActivity.java

public class MainActivity extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}

HomeFragment.java

public class HomeFragment extends Fragment {

private HomeViewModel homeViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);

ViewPager viewPager = root.findViewById(R.id.view_pager);
viewPager.setAdapter(new ViewPagerAdapter(getActivity().getSupportFragmentManager()));
return root;
}
}

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter {

public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return new BundyFragment(); //ChildFragment1 at position 0
case 1:
return new AnnouncementFragment(); //ChildFragment2 at position 1
case 2:
return new CalendarFragment(); //ChildFragment3 at position 2
default:
}
return null; //does not happen
}

@Override
public int getCount() {
return 3; //three fragments
}
}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

最佳答案

而不是使用

 viewPager.setAdapter(new ViewPagerAdapter(getActivity().getSupportFragmentManager()));

使用

viewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));

我遇到了这个问题,我使用上面的解决方案解决了它

关于java - 带 fragment 的抽屉导航(带 ViewPager)重新打开时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59332824/

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