[英]Switch tabs inside a Fragment from Drawer Menu
I have a TabLayout in a Fragment. 我在一个片段中有一个TabLayout。 I'm trying to switch tabs using DrawerLayout item. 我正在尝试使用DrawerLayout项目切换选项卡。 Not sure how to access the TabLayout from the parent Activity. 不确定如何从父活动访问TabLayout。 Checked everywhere, to no avail. 到处检查,无济于事。
MainActivity.java MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Toolbar and set it as the Action Bar
mToolbar = (Toolbar) findViewById(R.id.mToolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// Inflate the TabFragment as the first one
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
// Initialize Drawer Menu
mDrawerLayout = (DrawerLayout) findViewById(R.id.mDrawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.mNavigationView);
headerLayout = mNavigationView.getHeaderView(0);
// Set click events for the Drawer Menu items
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// Close the Drawer Menu when an item is clicked
mDrawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case R.id.menuLessons:
return true;
case R.id.menuCheatSheet:
return true;
case R.id.menuMyProfile:
startActivity(new Intent(MainActivity.this, MyProfileActivity.class));
overridePendingTransition(R.anim.zoom_in, R.anim.fade_out);
return true;
default:
return true;
}
}
});
}
}
TabFragment.java TabFragment.java
public class TabFragment extends Fragment {
public TabLayout mTabLayout;
public ViewPager mViewPager;
public static int int_items = 2;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the Tab Layout and setup View Pager
View x = inflater.inflate(R.layout.layout_tabs, null);
mTabLayout = (TabLayout) x.findViewById(R.id.mTabLayout);
mViewPager = (ViewPager) x.findViewById(R.id.mViewPager);
// Setup Adapter for the View Pager
mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
return x;
}
private class MyAdapter extends FragmentPagerAdapter {
MyAdapter(FragmentManager fm) {
super(fm);
}
// Return Fragment with respect to position
@Override
public Fragment getItem(int position) {
switch(position) {
case 0 : return new LessonsFragment();
case 1 : return new CheatSheetFragment();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
// Return title of the tab according to the position
@Override
public CharSequence getPageTitle(int position) {
switch(position) {
case 0 :
return "Lessons";
case 1 :
return "Cheat Sheet";
}
return null;
}
}
public void setCurrentTab(int tab_index) {
FragmentTabHost mTabHost = (FragmentTabHost)getActivity().findViewById(android.R.id.tabhost);
mTabHost.setCurrentTab(tab_index);
}
}
To get an instance to a Fragment in an activity, use: 要将实例获取为活动中的片段,请使用:
Fragment tabFragment = (TabFragment) getSupportFragmentManager().findFragmentById(R.id.containerView);
Once you have this, you can then call your method: 一旦有了这个,就可以调用方法了:
tabFragment.setCurrentTab(/*tab index*/);
That answers the question, however I would consider using a ViewPager or simply sticking with tapping tabs to move to them instead of doing it view the nav drawer. 回答了这个问题,但是我会考虑使用ViewPager或只是坚持点击选项卡将其移至它们,而不是查看导航抽屉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.