如何 link 每个选项卡到导航抽屉?
How to link each tabs to Navigation Drawer?
由于我是 android 的新手,我遇到了一个小问题,我实际上不知道该怎么做。实际上,我正在为每个选项卡寻找 link 的导航抽屉菜单。例如:滑动标签为 'tab1'、'tab2' 和 'tab3',导航抽屉菜单也列为 'tab1'、'tab2' 和 ;tab3'。因此,当我从导航抽屉中单击 'tab2' 时,它应该将我带到滑动选项卡的 'tab2'。有人可以帮帮我吗?
谢谢
//MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
list=LoadingContentActivity.list;
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionar=getSupportActionBar();
actionar.setDisplayHomeAsUpEnabled(true);
actionar.setHomeAsUpIndicator(R.drawable.ic_menu);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.tab_11) {
startActivity(new Intent(MainActivity.this, FragmentTab1.class));
} else if (menuItem.getItemId() == R.id.tab_22) {
startActivity(new Intent(MainActivity.this, FragmentTab2.class));
} else if (menuItem.getItemId() == R.id.tab_33) {
startActivity(new Intent(MainActivity.this, FragmentTab3.class));
}
return true;
}
});
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new FragmentTab1(), "CALLS");
adapter.addFragment(new FragmentTab2(), "CHATS");
adapter.addFragment(new FragmentTab3(), "CONTACTS");
viewPager.setAdapter(adapter);
}
//TabsPagerAdapter.java
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentTab1();
case 1:
return new FragmentTab2();
case 2:
return new FragmentTab3();
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
您不必再次启动相同的 activity,因为这会导致它被推入 backstack。我建议您只需更改 ViewPager
中的当前片段
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
//your code
mViewPager = (ViewPager) findViewById(R.id.viewpager);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.tab_11) {
mViewPager.setCurrentItem(0);
} else if (menuItem.getItemId() == R.id.tab_22) {
mViewPager.setCurrentItem(1);
} else if (menuItem.getItemId() == R.id.tab_33) {
mViewPager.setCurrentItem(2);
}
mDrawerLayout.closeDrawers();
return true;
}
});
}
由于我是 android 的新手,我遇到了一个小问题,我实际上不知道该怎么做。实际上,我正在为每个选项卡寻找 link 的导航抽屉菜单。例如:滑动标签为 'tab1'、'tab2' 和 'tab3',导航抽屉菜单也列为 'tab1'、'tab2' 和 ;tab3'。因此,当我从导航抽屉中单击 'tab2' 时,它应该将我带到滑动选项卡的 'tab2'。有人可以帮帮我吗? 谢谢
//MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
list=LoadingContentActivity.list;
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionar=getSupportActionBar();
actionar.setDisplayHomeAsUpEnabled(true);
actionar.setHomeAsUpIndicator(R.drawable.ic_menu);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.tab_11) {
startActivity(new Intent(MainActivity.this, FragmentTab1.class));
} else if (menuItem.getItemId() == R.id.tab_22) {
startActivity(new Intent(MainActivity.this, FragmentTab2.class));
} else if (menuItem.getItemId() == R.id.tab_33) {
startActivity(new Intent(MainActivity.this, FragmentTab3.class));
}
return true;
}
});
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new FragmentTab1(), "CALLS");
adapter.addFragment(new FragmentTab2(), "CHATS");
adapter.addFragment(new FragmentTab3(), "CONTACTS");
viewPager.setAdapter(adapter);
}
//TabsPagerAdapter.java
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentTab1();
case 1:
return new FragmentTab2();
case 2:
return new FragmentTab3();
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
您不必再次启动相同的 activity,因为这会导致它被推入 backstack。我建议您只需更改 ViewPager
中的当前片段 ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
//your code
mViewPager = (ViewPager) findViewById(R.id.viewpager);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.tab_11) {
mViewPager.setCurrentItem(0);
} else if (menuItem.getItemId() == R.id.tab_22) {
mViewPager.setCurrentItem(1);
} else if (menuItem.getItemId() == R.id.tab_33) {
mViewPager.setCurrentItem(2);
}
mDrawerLayout.closeDrawers();
return true;
}
});
}