MainActivity.java
==================
package com.sam.web.scrolltab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) findViewById(R.id.pagerId);
FragmentManager fragmentManager=getSupportFragmentManager();
CustomAdapter adapter=new CustomAdapter(fragmentManager);
viewPager.setAdapter(adapter);
}
}
class CustomAdapter extends FragmentStatePagerAdapter{
public CustomAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
if(position==0){
fragment=new FragmentOne();
} if(position==1){
fragment=new FragmentTwo();
} if(position==2){
fragment=new FragmentThree();
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if(position==0){
return "Tab1";
} else if(position==1){
return "Tab2";
} if(position==2){
return "Tab3";
}
return null;
}
}
===================================================================================
CustomAdapter.java
=======================
package com.sam.web.scrolltab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) findViewById(R.id.pagerId);
FragmentManager fragmentManager=getSupportFragmentManager();
CustomAdapter adapter=new CustomAdapter(fragmentManager);
viewPager.setAdapter(adapter);
}
}
class CustomAdapter extends FragmentStatePagerAdapter{
public CustomAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
if(position==0){
fragment=new FragmentOne();
} if(position==1){
fragment=new FragmentTwo();
} if(position==2){
fragment=new FragmentThree();
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if(position==0){
return "Tab1";
} else if(position==1){
return "Tab2";
} if(position==2){
return "Tab3";
}
return null;
}
}
==============================================================================
FragmentOne.java
====================
package com.sam.web.scrolltab;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}
}
=================================================================================
FragmentTwo.java
====================
package com.sam.web.scrolltab;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTwo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_two, container, false);
}
}
===================================================================================
FragmentThree.java
========================
package com.sam.web.scrolltab;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentThree extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_three, container, false);
}
}
==========================================================================================
==========================================================================================
Activity_main.xml
===================
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pagerId"
tools:context="com.sam.web.scrolltab.MainActivity">
<android.support.v4.view.PagerTitleStrip
android:background="#d622ff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:id="@+id/title"
></android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
==============================================================================================
fragment_fragment_one.xml
=========================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#22ffcb"
tools:context="com.sam.web.scrolltab.FragmentOne">
<!-- TODO: Update blank fragment layout -->
<TextView
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment one" />
</LinearLayout>
==============================================================================================
fragment_fragment_two.xml
===========================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#852277ff"
tools:context="com.sam.web.scrolltab.FragmentTwo">
<!-- TODO: Update blank fragment layout -->
<TextView
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment Two" />
</LinearLayout>
==============================================================================================
fragment_fragment_three.xml
=============================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8222ff"
tools:context="com.sam.web.scrolltab.FragmentThree">
<!-- TODO: Update blank fragment layout -->
<TextView
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment Three" />
</LinearLayout>