Tag Archives: swipe view

Hello all, How are you? Today I am going to show you one of the easiest way to do Swipe View within Tab Layout in an android application,also called View Pager when we slide or swipe the page of Android Application.
It is really very helpful to show information in front and transition between two or more activity is very easy.So lets dig inside how to create this project –
First of all take or create your project and give desire name you want.In this project we going to use View Pager within Tab Layout so that both work together and final output will become so cool and easy.
Whenever we want to use two different layout within a single layout we will use Layout Inflator and we will approach this technique via fragments.
You can checkout the final output here and move further towards coding part –

Now lets move to coding or my best Java part you have to make TabPagerAdapter class for Tabs , so that if you select any Tab which class should call like if I tab Movie Tab then its corresponding class i.e. Movie.java will call.This class should extends FragmentStatePagerAdapter class to use the features of fragments.

TabPagerAdapter.java

TabPagerAdapter.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

packageandroidarena.tab;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentStatePagerAdapter;

publicclassTabPagerAdapterextendsFragmentStatePagerAdapter{

publicTabPagerAdapter(FragmentManager fm){

super(fm);

// TODO Auto-generated constructor stub

}

@Override

publicFragment getItem(inti){

switch(i){

case0:

//Fragement for Movie Tab

returnnewMovies();

case1:

//Fragment for Music Tab

returnnewMusic();

case2:

//Fragment for video Tab

returnnewVideo();

}

returnnull;

}

@Override

publicintgetCount(){

// TODO Auto-generated method stub

return3;//No of Tabs you can give your number of tabs

}

}

Now on getItem() we choose can choose different class so create those classes also Movie.java,Music.java and Video.java.This class should extends Fragments.

Movie.java

Movies.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

packageandroidarena.tab;

import androidarena.tab.R;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

publicclassMoviesextendsFragment{

@Override

publicView onCreateView(LayoutInflater inflater,ViewGroup container,

Bundle savedInstanceState){

View mov=inflater.inflate(R.layout.movie_frag,container,false);

((TextView)mov.findViewById(R.id.textView)).setText("Movies List");

returnmov;

}}

Music.java

Music.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

packageandroidarena.tab;

import androidarena.tab.R;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

publicclassMusicextendsFragment{

@Override

publicView onCreateView(LayoutInflater inflater,ViewGroup container,

Bundle savedInstanceState){

View mus=inflater.inflate(R.layout.music_frag,container,false);

((TextView)mus.findViewById(R.id.textView)).setText("Music Tracks");

returnmus;

}}

Video.java

Video.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

packageandroidarena.tab;

import androidarena.tab.R;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

publicclassVideoextendsFragment{

@Override

publicView onCreateView(LayoutInflater inflater,ViewGroup container,

Bundle savedInstanceState){

View vid=inflater.inflate(R.layout.video_frag,container,false);

((TextView)vid.findViewById(R.id.textView)).setText("Video Section");

returnvid;

}}

Now You have to make or create your MainActivity.java in which you will call TabPagerAdapter class object and set it in to your project .It should extends FragmentActivity class.

One more thing we going to use ActionBar and set the tabs over it so we also going to call ActionBar class .