2017-08-15 43 views
1

我面临这个问题,其中MainActivity内有3个Tabs,每个Tab包含一个片段。当我点击抽屉下的一个项目来开始一个新的片段时,它会在MainActivity的Tabs视图中重叠。下面是我的代码:Android:在MainActivity下包含Tabs的多个片段的重叠问题

Fragment currentFragment; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 
          @Nullable Bundle savedInstanceState) { 

     viewInflate = inflater.inflate(R.layout.fragment_city, container, false); 

     return viewInflate; 
    } 

    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 

     // Handle navigation view item clicks here. 
     Intent i; 
     int id = item.getItemId(); 

     if (id == R.id.nav_home) { 
      TabLayout tl = (TabLayout) findViewById(R.id.main_tabs); 
      tl.setVisibility(View.VISIBLE); 
      showFragment(new HomeFragment()); 
      setTitle(R.string.app_name); 
     } 
     if (id == R.id.nav_about_us) { 
      TabLayout tl = (TabLayout) findViewById(R.id.main_tabs); 
      tl.setVisibility(View.GONE); 
      showFragment(new AboutUsFragment()); 
      setTitle(R.string.about_us); 
     } 
     if (id == R.id.nav_favorites) { 
      TabLayout tl = (TabLayout) findViewById(R.id.main_tabs); 
      tl.setVisibility(View.GONE); 
      showFragment(new FavoritesFragment()); 
      setTitle(R.string.favorites); 
     } 
     if (id == R.id.nav_settings) { 
      i = new Intent(this, SettingsActivity.class); 
      startActivity(i); 
     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    public void showFragment(Fragment fragment) { 

     if (currentFragment != null && fragment.getClass().equals(currentFragment.getClass())) 
      return; 

     currentFragment = fragment; 
     FragmentManager fragmentManager = this.getSupportFragmentManager(); 
     if (fragmentManager == null) 
      return; 

     FragmentTransaction ft = fragmentManager.beginTransaction(); 
     if (ft == null) 
      return; 

     ft.replace(R.id.content_frame, fragment).commitAllowingStateLoss(); 
    } 

下面是fragment_city.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/content_background" 
    tools:context="com.creationjunkies.fragments.CityFragment"> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v4.widget.SwipeRefreshLayout 
      android:id="@+id/swipe_refresh" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/cityBargainsRecyclerView" 
       android:scrollbars="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 
     </android.support.v4.widget.SwipeRefreshLayout> 
    </RelativeLayout> 

</FrameLayout> 

的XML代码和activity_main.xml中的代码是:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    android:background="@color/content_background" 
    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" 
    android:id="@+id/drawer_layout" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.creationjunkies.cities.MainActivity"> 

    <android.support.design.widget.CoordinatorLayout 
     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" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay"> 

      </android.support.v7.widget.Toolbar> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/main_tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

     </android.support.design.widget.AppBarLayout> 


     <LinearLayout 
      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" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" 
      android:orientation="vertical"> 

      <FrameLayout 
       android:id="@+id/frameTopAds" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
      </FrameLayout> 

      <FrameLayout 
       android:id="@+id/content_frame" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1"> 

       <android.support.v4.view.ViewPager 
        android:id="@+id/tab_container" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" /> 

      </FrameLayout> 

      <FrameLayout 
       android:id="@+id/frameAds" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
      </FrameLayout> 

     </LinearLayout> 

    </android.support.design.widget.CoordinatorLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

你的帮助深表感谢!

+0

您可以加入您的活动XML布局? –

+0

@MuthukrishnanRajendran我已经添加了XML布局代码。谢谢! –

回答

0

将片段背景设置为白色。 它会解决这个问题。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#ffffff" 
tools:context="com.creationjunkies.fragments.CityFragment"> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/swipe_refresh" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/cityBargainsRecyclerView" 
      android:scrollbars="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 
    </android.support.v4.widget.SwipeRefreshLayout> 
</RelativeLayout> 

+0

不,它没有帮助,因为我已经在发布这个问题之前尝试过它:) –

+0

你可以发送截图吗? – user2980415