public class PropertyAddActivity extends SherlockFragmentActivity implements OnPageChangeListener, TabListener {
private String TAG="AddActivity: ";
private FileCache fileCache;
private ViewPager mPager;
private ActionBar ab;
private static final int COUNT = 3;
static ArrayList<Integer> mSelectedPropertyType = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.property_add);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
mPager.setOnPageChangeListener(this);
ab = getSupportActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowHomeEnabled(false);
ab.setNavigationMode(NAVIGATION_MODE_TABS);
GlobalProperty.getInstance().product=new PropertyAdd();
ab.addTab(ab.newTab().setText("Property").setTabListener(this));
ab.addTab(ab.newTab().setText("Property Detail 1").setTabListener(this));
ab.addTab(ab.newTab().setText("Property Detail 2").setTabListener(this));
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
//listAttachImage.clear();
mSelectedPropertyType.clear();
super.onDestroy();
}
#Override
public void onPageScrolled(int position, float positionOffset,int positionOffsetPixels) {
//AppLog.logString(TAG+"onPageScrolled");
}
#Override
public void onPageSelected(int position) {
//AppLog.logString(TAG+"onPageSelected position"+position);
getSupportActionBar().setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
//AppLog.logString(TAG+"onPageScrollStateChanged ");
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabSelected position: "+tab.getPosition());
mPager.setCurrentItem(tab.getPosition());
//ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabUnselected");
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabReselected");
}
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
//AppLog.logString(TAG+"COUNT: "+COUNT);
return COUNT;
}
#Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
AppLog.logString(TAG+"position: "+position);
switch (position) {
case 0:
AppLog.logString(TAG+"FRAGGGG1111111");
f = Property1AddFragmentActivity.newInstance(position);
break;
case 1:
AppLog.logString(TAG+"FRAGGGG2222222");
f = Property2AddFragmentActivity.newInstance(position);
break;
case 2:
AppLog.logString(TAG+"FRAGGGGG333333");
f = Property3AddFragmentActivity.newInstance(position);
break;
default:
AppLog.logString(TAG+"Default");
break;
}
return f;
}
}}
i had add add 3 tab Now the problem is that when i set some data in tab1 view and move to tab2 the reselect tab1 the data on tab1 is as it is but when i move to tab3 to tab1 then tab1 data is clear. so cany anyof you have idea to solve this?
At the moment your code appears to be partly action bar tabs and partly ordinary fragments.
If you are trying to use action bar tabs then you need a tab listener. In onTabSelected you should add/replace and in onTabUnselected you should remove.
Please see http://developer.android.com/guide/topics/ui/actionbar.html
section "adding navigation tabs" for a good example.
Related
know anyone how can I add one back button one my action bar?Here it's my acplication and I want to add it before INFO button.
I've try to add that code for action bar but it don't works:
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setHomeButtonEnabled(true);
Here it's my activity:
public class TrailActivity extends FragmentActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
Intent intent;
Trail trail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intent = getIntent();
setContentView(R.layout.trail_fracment_main);
trail = intent.getParcelableExtra("trail");
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setHomeButtonEnabled(true);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, tell the ViewPager to switch to the corresponding page.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0) {
InfoFragments infoFragments = new InfoFragments();
infoFragments.setTrail(trail);
Fragment fragment = infoFragments;
return fragment;
} else if (position == 1) {
return new MapFragments();
} else {
return new WondersFragments();
}
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
}
EDITED:
In the left part it's how should be looks my action bar with back button and in the right part it's the actually my application right now. So.. How can I make it to looks like the aplication from left part?
If anyone know please show me how I can do it.
Thx :D
Implement OnBackStackChangedListener and add this code to your Fragment Activity.
#Override
public void onCreate(Bundle savedInstanceState) {
//Listen for changes in the back stack
getSupportFragmentManager().addOnBackStackChangedListener(this);
//Handle when activity is recreated like on orientation Change
shouldDisplayHomeUp();
}
#Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
boolean canback = getSupportFragmentManager().getBackStackEntryCount()>0;
getSupportActionBar().setDisplayHomeAsUpEnabled(canback);
}
#Override
public boolean onSupportNavigateUp() {
//This method is called when the up button is pressed. Just the pop back stack.
getSupportFragmentManager().popBackStack();
return true;
}
If you are looking for Back Arrow beside the ActionBar tabs.Using Sherlock you can do it.Try this
https://stackoverflow.com/a/12703960/2365507
In AppCompatActivity , I can add the code protected void onResume() to launch some function when UI is shown or restored.
Now I created a Tabbed Activity, I add the code protected void onResume() to PlaceholderFragmentOld class , and I hope the system do someting when the UI cleanup_delete_fragment_old is shown, but the system crash and return the following error.
android.support.v4.app.SuperNotCalledException: Fragment PlaceholderFragmentOld{40e55078 #0 id=0x7f0f0070 android:switcher:2131689584:0} did not call through to super.onResume()
PlaceholderFragmentOld.java
public class PlaceholderFragmentOld extends Fragment {
private AdView adView;
private SMSRange mSmsRange;
private View mView;
private Context mContext;
private Spinner spinnerRanger;
private CheckBox chIsWriteLog;
public PlaceholderFragmentOld() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.cleanup_delete_fragment_old, container, false);
mView=rootView;
mContext=rootView.getContext();
return rootView;
}
#Override
protected void onResume() {
DoSometing();
}
}
CleanupDelete.java
public class CleanupDelete extends ActionBarActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cleanup_delete);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment;
switch(position){
case 0:
fragment = new PlaceholderFragmentOld();
break;
case 1:
fragment = new PlaceholderFragmentReduce();
break;
case 2:
fragment = new PlaceholderFragmentTrim();
break;
case 3:
fragment = new PlaceholderFragmentPerson();
break;
default:
throw new IllegalArgumentException("Invalid section number");
}
return fragment;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.Tab_Old);
case 1:
return getString(R.string.Tab_Reduce);
case 2:
return getString(R.string.Tab_Trim);
case 3:
return getString(R.string.Tab_Person);
}
return null;
}
}
}
Yes, you can add onResume() to a Fragment.
The error your getting states:
SuperNotCalledException: PlaceholderFragmentOld did not call through to super.onResume()
You need to call super.onResume() as the first line in your onResume() method, otherwise the system will complain. Something like this would work
protected void onResume(){
super.onResume();
doSomething();
}
NoteActivity Code:
public class NoteActivity extends FragmentActivity implements ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Note", "Note Info"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
TabsPageAdapter.Java
public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index)
{
case 0:
return new NoteFragment();
case 1:
return new NoteInfoFragment();
}
return null;
}
#Override
public int getCount()
{
// get item count - equal to number of tabs
return 2;
}
}
So basically, I used an example of Tabs View and got the Tabs working and it looks like this:
What would I have to do to make it just swipeable without the Tabs showing. An example is like Snapchat. It definitely uses the Swipe view control but the tabs are hidden. Can someone please show me how to get this done?
Since you already have a ViewPager in your code, all you have to do is remove the code that creates the ActionBar tabs (under the comment // Adding tabs), as well as the code that synchronizes the tab selection with the current page (start with the ViewPager.OnPageChangeListener and the ActionBar.TabListener callbacks and see if anything breaks).
I have the following problem. I have an app with a viewpager and two tabs. The two tabs are two listfragments. Now I want that if the user selects an item in the first tab, that the 5th item is selected in the second tab. (the second tab is the translation of text from the first item). My problem is that when i select an item in the first tab, and now switch to the second tab, there is nothing selected.
When I click something in the first tab I call a function in the Parent activity which calls a function in the second fragment which should select the 5th Item.
This is the code of the Fragment activity:
public class Dailyquran extends FragmentActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
public ArrayList<Tweet> tweets = new ArrayList<Tweet>();
public ArrayList<Tweet> tweets2 = new ArrayList<Tweet>();
//private static ArrayList<String> roomList;
public String a;
public static int laenge_inhalt;
public static int selected;
public static String sure_media;
public static String vers_media;
String koran_filename;
//int tabwahl=0;
int playstatus=0;
MediaPlayer mp = new MediaPlayer();
MenuItem playMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_dailyquran, menu);
playMenu = menu.findItem(R.id.menu_play);
return true;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Daily Quran");
setContentView(R.layout.activity_dailyquran);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the action bar.
final ActionBar actionBar = getActionBar();
// set the app icon as an action to go home
actionBar.setDisplayHomeAsUpEnabled(true);
//enable tabs in actionbar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we have a reference to the
// Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by the adapter.
// Also specify this Activity object, which implements the TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
main();
}
/* WEITER UNTEN SIND DIE TABSELECTED FUNKTIONEN !!!!
//#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
// #Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
// #Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private FragmentTransaction mCurTransaction = null;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE; //To make notifyDataSetChanged() do something
}
#Override
public Fragment getItem(int i) {
// Fragment building_fragment = new BuildingFragment();
Fragment room_fragment = new RoomFragment();
Fragment transl_fragment = new TransliterationFragment();
// Fragment device_fragment = new DeviceFragment();
Bundle args = new Bundle();
switch(i){
case 0:
room_fragment.setArguments(args);
return room_fragment;
case 1:
return transl_fragment;
case 2:
// args.putInt(RoomFragment.ARG_SECTION_NUMBER, i);
// room_fragment.setArguments(args);
return room_fragment;
default: return null;
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return "a";
case 1: return "b";
case 2: return "c";
}
return null;
}
#Override
public Object instantiateItem(View container, int position) {
if (mCurTransaction == null) {
mCurTransaction = getSupportFragmentManager().beginTransaction();
}
// TODO Auto-generated method stub
Fragment fragment = getItem(position);
if (fragment!=null){
System.out.println("Fragment Found!");
mCurTransaction.attach(fragment);
}
return fragment;//super.instantiateItem(container, position);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_play:
play();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem switchButton = menu.findItem(R.id.menu_play);
}
public class Tweet {
String content;
String sure;
String vers;
}
public class Tweet2 {
String content;
String sure;
String vers;
}
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
//HERE IS THE CODE FOR SELECTING THE SECOND ITEM
public void one_changed()
{
TransliterationFragment fragment_meaning = (TransliterationFragment) getSupportFragmentManager().findFragmentById(R.id.myfragment2);
Toast.makeText(getApplicationContext(),"Change Selection", Toast.LENGTH_SHORT).show();
fragment_meaning.change_selected(); // do what updates are required
}
AND THIS IS THE RELEVANT CODE OF THE SECOND FRAGMENT, I can see the string "Hallo" is in the console, so the function is really called, but i the Item isnt selected.
public void change_selected()
{
System.out.println("Hallo");
ListView list=getListView();
list.setSelection(4);
}
Its working now when i add this code in the function on_changed in the parent activity:
TransliterationFragment fragment_meaning = (TransliterationFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.pager+":1");
fragment_meaning.change_selected(); // do what updates are required
I have a late requirement change that requires tab navigation. I have already using ActionBarSherlock in my application. Previously my UI consisted of Activities. However from what i read the TabBar requires Fragment Activities? How is there any way i can keep my existing code and implement the TabBar or would this require a complete re write of my application ? All of my activities extends SherlockActivity ?
Kind Regards,
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class NewMainActivity extends SherlockFragmentActivity implements ActionBar.TabListener,
ViewPager.OnPageChangeListener
{
private ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.textlayout);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));
mViewPager.setOnPageChangeListener(this);
//mViewPager.setPageMarginDrawable(R.drawable.border);
mViewPager.setPageMargin(16);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("First Tab").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Second Tab").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Third Tab").setTabListener(this));
}
#Override
public void onPageScrollStateChanged(int arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
// TODO Auto-generated method stub
}
private class MainPagerAdapter extends FragmentPagerAdapter
{
public MainPagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
switch (position)
{
case 0:
// return (mFragmentA = new FragmentA());
case 1:
// return (mFragmentB = new FragmentB());
case 2:
// return (mFragmentC = new FragmentC());
}
return null;
}
#Override
public int getCount()
{
return 3;
}
}
}
Of course you won't have to re-write your application. You just need some refractoring that's all.
First you should start by moving your code to SherlockFragment(s).
So let's say you have 3 Activities: ActivityA, ActivityB and ActivityC.
1 - Create 3 SherlockFragments: FragmentA, FragmentB and FragmentC.
2 - Move your code to the fragments and make the necessary modifications (e.g. Overrides, Context references, etc.).
3 - Now to setup your main FragmentActivity with Tab navigation:
public class MainActivity extends SherlockFragmentActivity implements
ActionBar.TabListener, ViewPager.OnPageChangeListener {
private FragmentA mFragmentA;
private FragmentB mFragmenB;
private FragmentC mFragmentC;
private ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new MainPagerAdapter(
getSupportFragmentManager()));
mViewPager.setOnPageChangeListener(this);
mViewPager.setPageMarginDrawable(R.drawable.border);
mViewPager.setPageMargin(16);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText(R.string.tab_a_title)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.tab_b_title)
.setTabListener(this));
actionBar.addTab(actionBar.newTab()
.setText(R.string.tab_c_title).setTabListener(this));
}
#Override
public void onPageSelected(int position) {
getSupportActionBar().setSelectedNavigationItem(position);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
private class MainPagerAdapter extends FragmentPagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return (mFragmentA = new FragmentA());
case 1:
return (mFragmentB = new FragmentB());
case 2:
return (mFragmentC = new FragmentC());
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
}
main_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>