Friday, 25 September 2015

Tab Activity With Swipe Gesture

Hello Guys,

Today I share an example of Tab implementation with Swipe Gesture.

All of you very well aware of Tabs. Because you see multiple tabs in many applications. Same way you can create different tabs into your application.

Using Swipe Gesture you can scroll your screen horizontally mean while your tabs are also changed.

Here I provide you sample code for it.

Sample Code:

File: MainActivity.java

package com.sneha.tabsswipe;

import com.sneha.tabsswipe.adapter.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;

        // Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initilization
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));
}

/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}

@Override
public void onPageScrollStateChanged(int arg0)
{
}
});
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
}
}

File: TabsPagerAdapter.java

package com.sneha.tabsswipe.adapter;

import com.sneha.tabsswipe.GamesFragment;
import com.sneha.tabsswipe.MoviesFragment;
import com.sneha.tabsswipe.TopRatedFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm)
{
super(fm);
}

@Override
public Fragment getItem(int index)
{
switch (index)
{
case 0:
// Top Rated fragment activity
return new TopRatedFragment();
case 1:
// Games fragment activity
return new GamesFragment();
case 2:
// Movies fragment activity
return new MoviesFragment();
}
return null;
}

@Override
public int getCount()
{
// get item count - equal to number of tabs
return 3;
}
}

File: TopRatedFragment.java

package com.sneha.tabsswipe;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TopRatedFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle                                 savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

return rootView;
}
}

File: GamesFragment.java

package com.sneha.tabsswipe;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class GamesFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle                                 savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_games, container, false);

return rootView;
}
}

File: MoviesFragment.java

package com.sneha.tabsswipe;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MoviesFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle                                 savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);

return rootView;
}
}

Output:












































































Download Full Source Code From Here: TabActivity

Happy Coding...!!!

No comments:

Post a Comment