How To Add Swipe In Tabs
Solution 1:
Create ViewPagerAdapter class-
publicclassViewPagerAdapterextendsFragmentStatePagerAdapter {
privatefinalintPAGES=4;
publicViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Overridepublic Fragment getItem(int position) {
switch (position) {
case0:
returnnewType1();
case1:
returnnewType2();
case2:
returnnewType3();
case3:
returnnewType4();
default:
thrownewIllegalArgumentException("The item position should be less or equal to:" + PAGES);
}
}
@OverridepublicintgetCount() {
return PAGES;
}
}
Then in your Typle class-
publicclassTypesextendsActionBarActivity {
privateViewPager viewPager;
@OverridepublicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.pagerad);
viewPager = (ViewPager) findViewById(R.id.pager);
ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowHomeEnabled(true);
viewPager.setOnPageChangeListener(onPageChangeListener);
viewPager.setAdapter(newViewPagerAdapter(getSupportFragmentManager()));
addActionBarTabs();
}
privateViewPager.SimpleOnPageChangeListener onPageChangeListener = newViewPager.SimpleOnPageChangeListener() {
@OverridepublicvoidonPageSelected(int position) {
super.onPageSelected(position);
ab.setSelectedNavigationItem(position);
}
};
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action barMenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
returnsuper.onCreateOptionsMenu(menu);
}
@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
homeActivity();
returntrue;
default:
returnsuper.onOptionsItemSelected(item);
}
}
publicvoidhomeActivity() {
Toast.makeText(this, "Home Option Selexted", Toast.LENGTH_SHORT).show();
}
privatevoidaddActionBarTabs() {
ab = getSupportActionBar();
String[] tabs = { "TYPE 1", "TYPE 2", "TYPE 3", "TYPE 4" };
for (String tabTitle : tabs) {
ActionBar.Tab tab = ab.newTab().setText(tabTitle).setTabListener(tabListener);
ab.addTab(tab);
}
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
privateActionBar.TabListener tabListener = newActionBar.TabListener() {
@OverridepublicvoidonTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@OverridepublicvoidonTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@OverridepublicvoidonTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
}
Finally create pagerad xml-
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
Then create Each Type fragment like-
publicclassType1extendsFragment{
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewrootView= inflater.inflate(R.layout.type, container, false)
return rootView;
}
}
Solution 2:
You need to use the ViewPager. When implementing the viewpager from the android tutorial you simply need to focus on the getitem method of the SectionsPagerAdapter. Here you could pick the right fragment based on the position of the position of the "view".
Your challenge is that the swipe part of the app is now one activity only, and each view now must be implemented as a fragment. But this layout seems compatible with your current code (eg similar to the onTabSelected method).
You types.xml file should be a ViewPager:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/pager"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.Types" />
Next, declare a ViewPager and SectionsPagerAdapter in you Types activity:
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
Now, in you OnCreate add:
// Create the adapter that will return a fragment for each swipe screen
mSectionsPagerAdapter = newSectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
The SectionsPagerAdapter is implemented as:
publicclassSectionsPagerAdapterextendsFragmentPagerAdapter {
publicSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Overridepublic Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.if (position == 2) {
returnnewFragmentA();
} elseif (position == 1) {
returnnewFragmentB();
} else {
// Pattern from you code...return Fragment.instantiate( mActivity, mFragName );
}
}
@OverridepublicintgetCount() {
// Show 3 total pages.return3;
}
@Overridepublic CharSequence getPageTitle(int position) {
Localel= Locale.getDefault();
switch (position) {
case0:
return getString(R.string.title_section1).toUpperCase(l);
case1:
return getString(R.string.title_section2).toUpperCase(l);
case2:
return getString(R.string.title_section3).toUpperCase(l);
}
returnnull;
}
}
That's all :)
Post a Comment for "How To Add Swipe In Tabs"