public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;//用于为ViewPager提供多个Fragment,通常用于被扩展
private ViewPager mViewPager;//是Fragment容器,可以同时管理多个Fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
LayoutInflater layout= this.getLayoutInflater();
final View myView=layout.inflate(R.layout.activity_main2,null);
final DrawerLayout drawer = (DrawerLayout) myView.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) myView.findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) myView.findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
);
}
public void onBackPressed() {
LayoutInflater layout= this.getLayoutInflater();
View myView=layout.inflate(R.layout.activity_main2,null);
DrawerLayout drawer = (DrawerLayout) myView.findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "haha";//键值
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
ImageView imageView= (ImageView) rootView.findViewById(R.id.image1);
imageView.setImageResource(R.drawable.e);
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
* 这个是在Toolbar上的页码被选择之后创建的
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
//获取第position位置的Fragment
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
//在选择的位置上创建一个新的Fragment
return PlaceholderFragment.newInstance(position + 1);
}
//表示Adapter总共包含多少个Fragment
@Override
public int getCount() {
//
return 4;
}
//该方法确定每一个Fragment的标题
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "第一页";
case 1:
return "第二页";
case 2:
return "第三页";
case 3:
return "第四页";
}
return null;
}
}
}
侧滑菜单的布局文件不包含在Activity中,现在的情况是单击Toolbar上的toggle,完全没有反应.....
+++++++++++++++++++++++++++++++++++++
如果我想同时显示顶部导航和侧滑菜单应该怎么做呢?还有一个问题,就是侧滑菜单是向右滑动呼出的,但是导航也可以通过左右滑动进行切换,怎么解决这个冲突呢?