Page 143

Android Programming Cookbook

135 / 143

android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" tools:context=".MainActivity"/> </LinearLayout>

8.3

Create the source code of the main AndroidViewPagerExample Activity

Add a new Java class inside src/com.javacodegeeks.androidViewPagerExample/ so that we are going to have the src/com.javacodegeeks.androidViewPagerExample/AndroidViewPagerExample.java file and paste the code below. AndroidViewPagerExample.java package com.javacodegeeks.examples.androidViewPagerExample; import import import import import

android.support.v4.app.FragmentManager; android.support.v4.app.FragmentPagerAdapter; android.os.Bundle; android.support.v4.view.ViewPager; android.support.v7.app.AppCompatActivity;

public class AndroidViewPagerExample extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); } private class MyPagerAdapter extends FragmentPagerAdapter { public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public android.support.v4.app.Fragment getItem(int pos) { switch (pos) { case 0: return FragmentViewPager.newInstance(getString(R.string.title_section1) , R.drawable.rock); case 1: return FragmentViewPager.newInstance(getString(R.string.title_section2) , R.drawable.paper); case 2: return FragmentViewPager.newInstance(getString(R.string.title_section3) , R.drawable.scissors); default: return FragmentViewPager.newInstance(getString(R.string.title_section1) , R.drawable.rock); } } @Override public int getCount() {

←-

←-

←-

←-

SKY MAGZ  

This is Yahya Christian School Magazine

Read more
Read more
Similar to
Popular now
Just for you