Created
October 26, 2013 16:03
-
-
Save tonsV2/7171135 to your computer and use it in GitHub Desktop.
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
package com.snot.findawatertap; | |
import android.os.Bundle; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.view.ViewPager; | |
import android.support.v4.app.FragmentTransaction; | |
import android.support.v7.app.ActionBarActivity; | |
import android.support.v7.app.ActionBar; | |
import android.content.Context; | |
import java.util.List; | |
import java.util.ArrayList; | |
/** | |
* Created by snot on 6/20/13. | |
*/ | |
//// Usage: | |
// | |
//public class MainActivity extends TabsFragmentActivity { | |
// @Override | |
// protected void onCreate(Bundle savedInstanceState) { | |
// super.onCreate(savedInstanceState); | |
// | |
// this.addTab(getString(R.string.tab_some_title), new SomeFragment()); | |
// this.addTab(getString(R.string.tab_some_other_title), new SomeOtherFragment()); | |
// this.addTab(getString(R.string.tab_yet_another_title), new YetAnotherFragment()); | |
// } | |
//} | |
// | |
public class TabsFragmentActivity extends ActionBarActivity implements ActionBar.TabListener | |
{ | |
ViewPager mViewPager; | |
TabsAdapter ta; | |
ActionBar actionBar; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.tabs_activity); | |
ta = new TabsAdapter(getSupportFragmentManager(), this); | |
actionBar = getSupportActionBar(); | |
final ActionBar finalActionBar = actionBar; | |
// Set up the action bar. | |
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); | |
// Setup view pager | |
mViewPager = (ViewPager)findViewById(R.id.pager); | |
mViewPager.setAdapter(ta); | |
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { | |
@Override | |
public void onPageSelected(int position) { | |
// When swiping between different app sections, select the corresponding tab. | |
// We can also use ActionBar.Tab#select() to do this if we have a reference to the | |
// Tab. | |
finalActionBar.setSelectedNavigationItem(position); | |
} | |
}); | |
} | |
public void addTab(int id, Class<?> cls) | |
{ | |
addTab(getString(id), cls); | |
} | |
public void addTab(String title, Class<?> cls) | |
{ | |
ta.addTab(cls); | |
actionBar.addTab(actionBar.newTab().setText(title).setTabListener(this)); | |
ta.notifyDataSetChanged(); | |
} | |
@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()); | |
} | |
// public Fragment getCurrent() | |
// { | |
// return current; | |
// } | |
@Override | |
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { | |
} | |
private static class TabsAdapter extends FragmentPagerAdapter { | |
private Context context; | |
private List<Class<?>> fragments = new ArrayList<Class<?>>(); | |
public TabsAdapter(FragmentManager fragmentManager, Context context) { | |
super(fragmentManager); | |
this.context = context; | |
} | |
public void addTab(Class<?> cls) | |
{ | |
fragments.add(cls); | |
} | |
@Override | |
public int getCount() { | |
return fragments.size(); | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return Fragment.instantiate(context, fragments.get(position).getName()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment