Created
January 15, 2014 05:06
-
-
Save ishitcno1/8431133 to your computer and use it in GitHub Desktop.
Create swipe views with tabs in android.
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
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
<android.support.v4.view.ViewPager | |
android:id="@+id/pager" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</FrameLayout> |
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 ActionBarActivity { | |
private ActionBar mActionBar; | |
private ViewPager mViewPager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mActionBar = (ActionBar) getSupportActionBar(); | |
mViewPager = (ViewPager) findViewById(R.id.pager); | |
mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager())); | |
mViewPager.setOnPageChangeListener(new SimpleOnPageChangeListener() { | |
@Override | |
public void onPageSelected(int position) { | |
mActionBar.setSelectedNavigationItem(position); | |
} | |
}); | |
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); | |
ActionBar.Tab tab = mActionBar.newTab() | |
.setText("Hot") | |
.setTabListener(new MyTabListener<HotFragment>( | |
this, "hot", HotFragment.class, mViewPager)); | |
mActionBar.addTab(tab); | |
tab = mActionBar.newTab() | |
.setText("category") | |
.setTabListener(new MyTabListener<CategoryFragment>( | |
this, "category", CategoryFragment.class, mViewPager)); | |
mActionBar.addTab(tab); | |
} | |
} | |
MyFragmentPagerAdapter.java | |
public class MyFragmentPagerAdapter extends FragmentPagerAdapter { | |
public MyFragmentPagerAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
@Override | |
public Fragment getItem(int i) { | |
Fragment fragment = null; | |
if (i == 0) { | |
fragment = new HotFragment(); | |
} else if (i == 1){ | |
fragment = new CategoryFragment(); | |
} | |
return fragment; | |
} | |
@Override | |
public int getCount() { | |
return 2; | |
} | |
} |
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 MyTabListener<T extends Fragment> implements TabListener { | |
private Activity mActivity; | |
private Fragment mFragment; | |
private String mTag; | |
private Class<T> mClass; | |
private ViewPager mViewPager; | |
public MyTabListener(Activity activity, String tag, Class<T> clz, | |
ViewPager viewPager) { | |
mActivity = activity; | |
mTag = tag; | |
mClass = clz; | |
mViewPager = viewPager; | |
} | |
@Override | |
public void onTabReselected(Tab arg0, FragmentTransaction arg1) { | |
} | |
@Override | |
public void onTabSelected(Tab tab, FragmentTransaction ft) { | |
if (mFragment == null) { | |
mFragment = Fragment.instantiate(mActivity, mClass.getName()); | |
ft.add(android.R.id.content, mFragment, mTag); | |
} else { | |
ft.attach(mFragment); | |
} | |
mViewPager.setCurrentItem(tab.getPosition()); | |
} | |
@Override | |
public void onTabUnselected(Tab tab, FragmentTransaction ft) { | |
if (mFragment != null) { | |
ft.detach(mFragment); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment