2017-08-15 42 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