Skip to content

Instantly share code, notes, and snippets.

@unusualbob
Created October 18, 2012 14:07
Show Gist options
  • Save unusualbob/3912057 to your computer and use it in GitHub Desktop.
Save unusualbob/3912057 to your computer and use it in GitHub Desktop.
Basic fragment stuff
public class MainActivity extends FragmentActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
mainID = this.findViewById(R.id.mainid);
mAdapter = new MainFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
//This is the start page
mIndicator.setCurrentItem(1);
}
}
public final class MyFirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myview = inflater.inflate(R.layout.myview, null);
return myview;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
public static Fragment newInstance()
{
MyFirstFragment fragment = new MyFirstFragment();
return fragment;
}
}
public class MainFragmentAdapter extends FragmentPagerAdapter implements TitleProvider{
protected static final String[] CONTENT = new String[] {"Fragment Title One","Fragment Title Two", "Fragment Title Three" };
private int mCount = CONTENT.length;
public MainFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return MyFirstFragment.newInstance();
case 1:
return MySecondFragment.newInstance();
}
}
@Override
public String getTitle(int position) {
return CONTENT[position % CONTENT.length];
}
@Override
public String getPageTitle(int position) {
return CONTENT[position % CONTENT.length];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment