Created
October 18, 2012 14:07
-
-
Save unusualbob/3912057 to your computer and use it in GitHub Desktop.
Basic fragment stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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