2016-08-31 205 views
0

我正在做一個項目,我想顯示不同的片段,這取決於我在導航視圖中選擇的位置。問題是它從不顯示它必須顯示的片段。關於導航項目選擇不顯示片段與選項卡布局

導航視圖的視圖稱爲activity_main,從那裏您可以到達片段管理器使用的佈局content_main。

的問題是,這兩個選項卡都沒有集成在MainActivity

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

    <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="com.blindsiot.carlos.blindsiot.MainActivity"> 

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

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

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

content_main的工具欄.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.blindsiot.carlos.blindsiot.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

</RelativeLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

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

    navigationView.setCheckedItem(R.id.nav_principal); 

    /* (. . .) */ 


    @Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    Fragment currFragment = null; 
    FragmentManager fragmentManager = getSupportFragmentManager(); 

    if (id == R.id.nav_principal) { 
     currFragment = new FastAccessFragment(); 
    } else if (id == R.id.nav_planning) { 
     currFragment = new SignUp(); 
    } else if (id == R.id.nav_settings) { 

    } else if (id == R.id.nav_help) { 

    } else if (id == R.id.nav_logout) { 
     editor.putBoolean("signedUp", false); 
     editor.apply(); 
     Intent intent = new Intent(this, LoginActivity.class); 
     if (intent != null) { 
      this.finish(); 
      startActivity(intent); 
     } 
    } else if (id == R.id.nav_about) { 

    } 

    if (currFragment != null) { 
     fragmentManager 
       .beginTransaction() 
       .replace(R.id.content_main, currFragment) 
       .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) 
       .commit(); 
    } 

    setTitle(item.getTitle()); 


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

    return true; 
} 

FastAccessFragment.java - 這是我在哪裏其實並在那裏我不明白爲什麼它不工作

public class FastAccessFragment extends Fragment { 

private ViewPager viewPager; 
View rootview; 
TabLayout tabLayout; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    rootview = inflater.inflate(R.layout.fast_access_fragment, container, false); 
    tabLayout = (TabLayout) rootview.findViewById(R.id.tabLayout_fragment); 

    tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.action_sign_in_short)).setIcon(R.drawable.tab_login)); 
    tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.action_sign_up_short)).setIcon(R.drawable.tab_user)); 
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

    viewPager = (ViewPager) rootview.findViewById(R.id.pager_fragment); 

    PagerAdapter pagerAdapter = new PagerAdapter(getChildFragmentManager(), tabLayout.getTabCount()); 
    viewPager.setAdapter(pagerAdapter); 

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 

    tabLayout.post(new Runnable() { 
     @Override 
     public void run() { 
      tabLayout.setupWithViewPager(viewPager); 
      tabLayout.getTabAt(0).setText("test"); 
      tabLayout.getTabAt(1).setText("test2"); 
     } 
    }); 

    //Adding onTabSelectedListener to swipe views 
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 

    return rootview; 
} 

public class PagerAdapter extends FragmentStatePagerAdapter { 
    int mNumOfTabs; 

    public PagerAdapter(FragmentManager fm, int NumOfTabs) { 
     super(fm); 
     this.mNumOfTabs = NumOfTabs; 
    } 

    @Override 
    public Fragment getItem(int position) { 

     switch (position) { 
      case 0: 
       SignUp tab1 = new SignUp(); 
       return tab1; 
      case 1: 
       SignIn tab2 = new SignIn(); 
       return tab2; 
      default: 
       return null; 
     } 
    } 

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

    } 
+0

你能解釋一下更多的行爲嗎..... – shalini

回答

相關問題