Skip to content

Instantly share code, notes, and snippets.

@YvesBill
Last active December 21, 2015 04:48
Show Gist options
  • Save YvesBill/6251835 to your computer and use it in GitHub Desktop.
Save YvesBill/6251835 to your computer and use it in GitHub Desktop.
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Settare il tipo di navigazione
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
// Il return new chiama le altre classi (i Fragment)
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Sector1();
case 1:
return new Sector2();
default:
Fragment fragment = new Sector1();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
}
// Settare il titolo dei Sector
@Override
public int getCount() {
return 2;
}
public CharSequence getPageTitle(int position) {
String tabLabel = null;
switch (position) {
case 0:
tabLabel = getString(R.string.sector1);
break;
case 1:
tabLabel = getString(R.string.sector2);
break;
}
return tabLabel;
}
}
public class Sector1 extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sector1, container, false);
...
return rootView;
}
}
//Lo stesso codice è utilizzato su Sector2.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment