ViewPager Demo
- mainactivity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<Button
android:id="@+id/buttonOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/Green"
android:text="@string/One"
android:textColor="@android:color/white" />
<Button
android:id="@+id/buttonTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/Yellow"
android:text="@string/Two"
android:textColor="@android:color/black" />
<Button
android:id="@+id/buttonThree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/Red"
android:text="@string/Three"
android:textColor="@android:color/white" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/myTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
___________________________________________________________________________
- One.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/Green"
android:padding="5dp"
android:text="@string/FragmentOne"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white" />
</LinearLayout>
___________________________________________________________________________
- One.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class One extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.one, container, false);
return rootView;
}
}
___________________________________________________________________________
- two.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/Yellow"
android:padding="5dp"
android:text="@string/FragmentTwo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/black" />
</RelativeLayout>
___________________________________________________________________________
- Two.java
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.viewpagerdemo.R;
public class Two extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.two, container, false);
return rootView;
}
}
___________________________________________________________________________
- three.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/Red"
android:padding="5dp"
android:text="@string/FragmentThree"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white" />
<Button
android:id="@+id/myThreePageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="150dp"
android:text="@string/FragmentThree" />
</RelativeLayout>
</ScrollView>
___________________________________________________________________________
- Three.java
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.example.viewpagerdemo.R;
public class Three extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.three, container, false);
Button btn = (Button) rootView.findViewById(R.id.myThreePageButton);
return rootView;
}
}
___________________________________________________________________________
- MyTabAdapter.java
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyTabAdapter extends FragmentPagerAdapter {
public MyTabAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int pageCount) {
switch (pageCount) {
case 0:
return new One();
case 1:
return new Two();
case 2:
return new Three();
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
___________________________________________________________________________
- PageTabClickListener.java
import android.view.View;
import android.view.View.OnClickListener;
public class PageTabClickListener implements OnClickListener {
private int pageIndex;
private ViewPager viewPager;
public PageTabClickListener(int pageIndex, ViewPager viewPager) {
this.viewPager = viewPager;
this.pageIndex = pageIndex;
}
public PageTabClickListener() {
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public void setViewPager(ViewPager viewPager) {
this.viewPager = viewPager;
}
@Override
public void onClick(View v) {
viewPager.setCurrentItem(pageIndex);
}
}
- MainActivity.java
import com.example.viewpagerdemo.R;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
Button buttonOne, buttonTwo, buttonThree;
buttonOne = (Button) findViewById(R.id.buttonOne);
buttonTwo = (Button) findViewById(R.id.buttonTwo);
buttonThree = (Button) findViewById(R.id.buttonThree);
ViewPager viewPager = (ViewPager) findViewById(R.id.myTab);
MyTabAdapter tabAdapter = new MyTabAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabAdapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
buttonOne.setOnClickListener(new PageTabClickListener(0,viewPager));
buttonTwo.setOnClickListener(new PageTabClickListener(1,viewPager));
buttonThree.setOnClickListener(new PageTabClickListener(2,viewPager));
}
}
___________________________________________________________________________- colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Green">#008000</color>
<color name="Red">#97072D</color>
<color name="Yellow">#FFD700</color>
</resources>
___________________________________________________________________________
- strings.xml
<resources>
<string name="app_name">ViewPagerDemo</string>
<string name="One">One</string>
<string name="Two">Two</string>
<string name="Three">Three</string>
<string name="FragmentOne">Fragment One</string>
<string name="FragmentTwo">Fragment Two</string>
<string name="FragmentThree">Fragment Three</string>
</resources>
OutPut: