2016-01-13 107 views
6

我對於我的糟糕代碼和糟糕的知識事先抱歉。我剛剛開始在Android和Java編碼Viewpager Tabs片段和正常碎片

我的問題是我有導航抽屜和製表符。雖然如果我從導航抽屜中選擇一個加載在Fragment中的東西,它不會顯示。只顯示標籤。

我的計劃是,一個導航抽屜選項卡內部應有3個選項卡,其餘部分只有普通頁面沒有選項卡。

MainActivity.java

http://pastebin.com/TV7aWy9c

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <FrameLayout 
     android:id="@+id/fragment_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#000000" /> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <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> 

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    tools:context="se.themeister.hello.MainActivity"> 

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

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

     <android.support.design.widget.TabLayout 
      android:id="@+id/tab_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/toolbar" 
      android:background="?attr/colorPrimary" 
      android:elevation="6dp" 
      android:minHeight="?attr/actionBarSize" 
      android:theme="@style/AppTheme.AppBarOverlay"/> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:background="#009900" 
      android:layout_below="@id/tab_layout"/> 

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

    <include layout="@layout/content_main"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

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

我不知道這是否足夠,或者瞭解更多信息或代碼需要

回答

11

您的代碼存在的問題是您的ViewPagerTabLayout應該是Fragment的一部分,而不是Activity的一部分。並且NavigationDrawer應該只是在視口中(即在容器中)用另一個替換一個片段。現在,在您的代碼中,您嘗試以某種方式將新的Fragment插入到ViewPager中。

Here's與導航抽屜一個非常基本的應用程序,一個片段與標籤和休息片段而不接片,其可以作爲一個例子使用:

enter image description here

Activity.xml

<android.support.v4.widget.DrawerLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <android.support.design.widget.CoordinatorLayout 
     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" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

     <FrameLayout 
      android:id="@+id/fragmentContainer" 
      android:layout_marginTop="?attr/actionBarSize" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

    </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> 

活動代碼:

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 

     getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).commit(); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.fragment_1) { 
      getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).commit(); 
     } else if (id == R.id.fragment_2) { 
      getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithoutTabs()).commit(); 
     } else if (id == R.id.fragment_3) { 
      getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new Fragment()).commit(); 
     } 

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

即,如您所見,我將替換FrameLayout中的片段,其編號爲fragmentContainer

涉及與標籤片段的所有邏輯,在FragmentWithTabs incapsulated:

public class FragmentWithTabs extends Fragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_with_tabs, container, false); 
     ViewPager viewPager = (ViewPager)rootView.findViewById(R.id.viewPager); 
     TabLayout tabLayout = (TabLayout)rootView.findViewById(R.id.tabLayout); 

     viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) { 
      @Override 
      public Fragment getItem(int position) { 
       return new SubFragment(position == 0? Color.BLUE : position == 1? Color.WHITE : Color.RED); 
      } 

      @Override 
      public CharSequence getPageTitle(int position) { 
       return position+""; 
      } 

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

     tabLayout.setupWithViewPager(viewPager); 
     return rootView; 
    } 
} 

我希望,它有助於

+0

我一直在關注同樣的做法很好,但有一個缺點,我也期待修理。理想情況下,「工具欄」和「TabLayout」應該具有相同的顏色,但使用上述方法會在「TabLayout」和「工具欄」之間產生陰影,這並不理想。 – Sharj