当我从导航视图的菜单之间切换时,我面临片段重叠的问题..最初,我的应用程序具有带有多个选项的导航视图..在该主页选项片段中,视图分页器包含两个选项卡..当我在视图分页器选项卡之间切换时效果很好..
问题是,当我从导航菜单(如“设置”)切换菜单时,相应的片段加载良好,然后我重新加载了首页片段,视图分页器加载了选项卡,但是旧片段(设置)显示在背景中
另外,当我加载几次home选项时,片段会重叠几次,而不是替换Fragment
我的代码如下
活动类:DashboardActivity.java
/** * DashboardActivity.java */public class DashboardActivity extends AppCompatActivity implements NavigationVIEw.OnNavigationItemSelectedListener, FragmentManager.OnBackStackChangedListener, VIEwPagelistener { /** * Used as initializing the layout as data binding. */ private ActivityDashboardBinding activityDashboardBinding; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); activityDashboardBinding = DataBindingUtil.setContentVIEw(this, R.layout.activity_dashboard); activityDashboardBinding.setonClickController(new DashboardController()); setSupportActionbar(activityDashboardBinding.toolbar); getSupportActionbar().setdisplayShowTitleEnabled(false); ActionbarDrawerToggle toggle = new ActionbarDrawerToggle( this, activityDashboardBinding.drawerLayout, activityDashboardBinding.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); activityDashboardBinding.drawerLayout.addDrawerListener(toggle); activityDashboardBinding.navigationVIEw.setNavigationItemSelectedListener(this); toggle.syncState(); getSupportActionbar().setdisplayHomeAsUpEnabled(true); getSupportActionbar().setDefaultdisplayHomeAsUpEnabled(false); toggle.setDrawerIndicatorEnabled(false); toggle.setHomeAsUpIndicator(R.drawable.ic_menu_hamburger); toggle.setToolbarNavigationClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if (activityDashboardBinding.drawerLayout.isDrawerOpen(GravityCompat.START)) { activityDashboardBinding.drawerLayout.closeDrawer(GravityCompat.START); } else { activityDashboardBinding.drawerLayout.openDrawer(GravityCompat.START); } } }); displaySelectedScreen(R.ID.nav_home); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_dashboard, menu); return true; } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroIDManifest.xml. int ID = item.getItemID(); if (ID == R.ID.action_notification) { startActivity(new Intent(this, NotificationActivity.class)); return true; } return super.onoptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @OverrIDe public boolean onNavigationItemSelected(@NonNull MenuItem item) { // Handle navigation vIEw item clicks here. displaySelectedScreen(item.getItemID()); return true; } /** * Method used as navigation selection option. * * @param itemID Selected ID. */ private voID displaySelectedScreen(int itemID) { Fragment fragment = null; String fragmentname = null; if (itemID == R.ID.nav_home) { activityDashboardBinding.toolbarTitle.setText(R.string.toolbar_name_dashboard); activityDashboardBinding.addNewRIDe.show(); fragment = new HomePageFragment(); fragmentname = Constants.name_NAVIGATION_DASHBOARD; } else if (itemID == R.ID.nav_rIDes) { } else if (itemID == R.ID.nav_profile) { activityDashboardBinding.toolbarTitle.setText(R.string.toolbar_name_my_profile); activityDashboardBinding.addNewRIDe.hIDe(); fragment = new UserProfileFragment(); fragmentname = Constants.name_NAVIGATION_MY_PROfile; } else if (itemID == R.ID.nav_settings) { activityDashboardBinding.toolbarTitle.setText(R.string.toolbar_name_settings); fragment = new SettingsFragment(); activityDashboardBinding.addNewRIDe.hIDe(); fragmentname = Constants.name_NAVIGATION_SETTINGS; } else if (itemID == R.ID.nav_logout) { Intent logoutIntent = new Intent(getApplicationContext(), LoginActivity.class); logoutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(logoutIntent); } if (fragment != null) { FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.addOnBackStackChangedListener(this); fragmentManager.beginTransaction().add(R.ID.container, fragment) .addToBackStack(fragmentname).commit(); } DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); drawer.closeDrawer(GravityCompat.START); } @OverrIDe public voID onBackpressed() { if (activityDashboardBinding.drawerLayout.isDrawerOpen(GravityCompat.START)) { activityDashboardBinding.drawerLayout.closeDrawer(GravityCompat.START); } else if (getFragmentname().equals(Constants.name_NAVIGATION_DASHBOARD)) { finish(); } else { // Let super handle the back press super.onBackpressed(); } } @OverrIDe public voID onBackStackChanged() { if (getFragmentname().equals(Constants.name_NAVIGATION_DASHBOARD)) { activityDashboardBinding.toolbarTitle.setText(R.string.toolbar_name_dashboard); activityDashboardBinding.addNewRIDe.show(); } else if (getFragmentname().equals(Constants.name_NAVIGATION_MY_PROfile)) { activityDashboardBinding.toolbarTitle.setText(R.string.toolbar_name_my_profile); activityDashboardBinding.addNewRIDe.hIDe(); } else { activityDashboardBinding.toolbarTitle.setText(R.string.toolbar_name_settings); activityDashboardBinding.addNewRIDe.hIDe(); } } /** * Method used to get the fragment transaction name to IDentify the fragment. * * @return the fragment name. */ private String getFragmentname() { FragmentManager fm = getSupportFragmentManager(); return fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1).getname(); } @OverrIDe public voID onVIEwPagelistener() { activityDashboardBinding.addNewRIDe.animate().translationY(0).setInterpolator(new linearInterpolator()).start(); }}
DashboardVIEwAdapter.java
/** * The class that contains the List of fragments and List of Title. We can use the fragments using * vIEw pager from here. * * @version 1.0 */public class DashboardVIEwAdapter extends FragmentStatePagerAdapter { /** * Title of the fragment List. */ private String[] mTitle; /** * Fragment List Which contains the fragments on the adapter. */ private List<Fragment> fragmentList; /** * Instantiates a new adapter dashboard vIEw. * * @param fm the Instance of the FragmentManager. */ public DashboardVIEwAdapter(FragmentManager fm) { super(fm); } /** * Set the List of Title from the activity. * * @param Titles Title List. */ public voID setTitle(String[] Titles) { this.mTitle = Titles; } /** * Set the fragment List for the particular vIEw pager using FragmentStatePagerAdapter. * * @param fragmentList List of fragment. */ public voID setFragmentList(List<Fragment> fragmentList) { this.fragmentList = fragmentList; } @OverrIDe public Fragment getItem(int position) { return fragmentList.get(position); } @OverrIDe public int getCount() { return mTitle.length; } @OverrIDe public CharSequence getPageTitle(int position) { return mTitle[position]; }}
HomePageFragment.java
/** * Fragment used as home page to display the rIDe offered and my rIDes details. * * @version 1.0 */public class HomePageFragment extends Fragment implements VIEwPager.OnPagechangelistener { /** * Binding the fragment. */ private FragmentHomePageBinding homePageBinding; /** * Interface to Listen the vIEw pager changes. */ private VIEwPagelistener vIEwPagelistener; @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { homePageBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home_page, container, false); setTabs(); setHasOptionsMenu(true); vIEwPagelistener = (VIEwPagelistener) getActivity(); return homePageBinding.getRoot(); } /** * Set up the RIDe offers and My rIDes fragments in the tab */ private voID setTabs() { String[] mTitle = new String[]{"RIDes offered", "My RIDes"}; FragmentManager fm = getChildFragmentManager(); DashboardVIEwAdapter mAdapter = new DashboardVIEwAdapter(fm); mAdapter.setTitle(mTitle); mAdapter.setFragmentList(getFragmentList()); homePageBinding.vIEwPager.setAdapter(mAdapter); homePageBinding.tabLayout.setupWithVIEwPager(homePageBinding.vIEwPager); homePageBinding.vIEwPager.addOnPagechangelistener(this); } /** * Get the fragment List to display the vIEw pager tabs. Recent chat and contacts fragment will * be return from this. * * @return List of the fragments for the vIEwpager */ private List<Fragment> getFragmentList() { /** * Add the fragment as a List. */ List<Fragment> fragmentList = new ArrayList<>(); /** The fragment contacts which contains the rIDe offer List. */ RIDesOfferedFragment rIDesOfferedFragment = new RIDesOfferedFragment(); /** The fragment contacts which contains the rIDe offer List. */ MyRIDesFragment myRIDesFragment = new MyRIDesFragment(); fragmentList.add(rIDesOfferedFragment); fragmentList.add(myRIDesFragment); return fragmentList; } @OverrIDe public voID onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { vIEwPagelistener.onVIEwPagelistener(); } @OverrIDe public voID onPageSelected(int position) { //OverrIDden Method } @OverrIDe public voID onPageScrollStateChanged(int state) { //OverrIDden Method } @OverrIDe public voID onPrepareOptionsMenu(Menu menu) { menu.findItem(R.ID.action_notification).setVisible(true); super.onPrepareOptionsMenu(menu); }}
activity_dashboard.xml
<?xml version="1.0" enCoding="utf-8"?><layout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools"> <data> <variable name="onClickController" type="com.contus.carpooling.dashboard.homepage.viewmodel.DashboardController" /> </data> <androID.support.v4.Widget.DrawerLayout androID:ID="@+ID/drawer_layout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:fitsSystemwindows="true" tools:openDrawer="start"> <androID.support.design.Widget.CoordinatorLayout androID:ID="@+ID/coordinator_layout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:fitsSystemwindows="true" tools:context="com.contus.carpooling.dashboard.homepage.vIEw.DashboardActivity"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <androID.support.v7.Widget.Toolbar androID:ID="@+ID/toolbar" androID:layout_wIDth="match_parent" androID:layout_height="?attr/actionbarSize" androID:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enteralways" app:popuptheme="@style/Apptheme.PopupOverlay"> <TextVIEw androID:ID="@+ID/tool_bar_Title" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:text="Dashboard" androID:textcolor="@androID:color/white" androID:textSize="18sp" /> </androID.support.v7.Widget.Toolbar> <FrameLayout androID:ID="@+ID/container" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_vIEw_behavior" /> </linearLayout> <androID.support.design.Widget.floatingActionbutton androID:ID="@+ID/add_new_rIDe" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="right|bottom" androID:layout_margin="10dp" androID:clickable="true" androID:onClick="@{onClickController.fabBtnOnClick()}" androID:src="@drawable/ic_action_add" app:backgroundTint="@color/colorSecondarycolor" app:fabSize="normal" app:layout_behavior="com.contus.carpooling.vIEw.ScrollAwareFABBehavior" /> </androID.support.design.Widget.CoordinatorLayout> <androID.support.design.Widget.NavigationVIEw androID:ID="@+ID/navigation_vIEw" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_gravity="start" androID:background="@androID:color/white" androID:fitsSystemwindows="true" app:headerLayout="@layout/navigation_header" app:itemIconTint="@color/colorPrimary" app:itemTextcolor="#4b4b4b" app:menu="@menu/activity_dashboard_drawer" app:theme="@style/NavigationDrawerSelected" /> </androID.support.v4.Widget.DrawerLayout></layout>
fragment_home_page.xml
<layout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools"> <data /><FrameLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context="com.contus.carpooling.dashboard.homepage.vIEw.HomePageFragment"> <androID.support.design.Widget.TabLayout androID:ID="@+ID/tab_layout" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="@color/colorPrimary" androID:minHeight="?attr/actionbarSize" androID:theme="@style/themeOverlay.AppCompat.Dark.Actionbar" app:tabIndicatorcolor="@color/colorHighlightBlue" app:tabIndicatorHeight="4dp" /> <androID.support.v4.vIEw.VIEwPager androID:ID="@+ID/vIEw_pager" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_vIEw_behavior" /> </linearLayout></FrameLayout></layout>
设置显示在仪表板的背景中.
等待回应…预先致谢
解决方法:
在这里,问题是添加了将背景设为透明的片段,因此将背景设置为布局片段文件
例如:
<FrameLayoutandroID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:background="@color/colorBackground">
总结 以上是内存溢出为你收集整理的MVVM:在具有导航视图的视图寻呼机中出现片段重叠问题?全部内容,希望文章能够帮你解决MVVM:在具有导航视图的视图寻呼机中出现片段重叠问题?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)