1 /* 2 * Copyright 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.animationsdemo; 18 19 import android.app.Fragment; 20 import android.app.FragmentManager; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.support.v13.app.FragmentStatePagerAdapter; 24 import android.support.v4.app.FragmentActivity; 25 import android.support.v4.app.NavUtils; 26 import android.support.v4.view.PagerAdapter; 27 import android.support.v4.view.ViewPager; 28 import android.view.Menu; 29 import android.view.MenuItem; 30 31 /** 32 * Demonstrates a "screen-slide" animation using a {@link ViewPager}. Because {@link ViewPager} 33 * automatically plays such an animation when calling {@link ViewPager#setCurrentItem(int)}, there 34 * isn't any animation-specific code in this sample. 35 * 36 * <p>This sample shows a "next" button that advances the user to the next step in a wizard, 37 * animating the current screen out (to the left) and the next screen in (from the right). The 38 * reverse animation is played when the user presses the "previous" button.</p> 39 * 40 * @see ScreenSlidePageFragment 41 */ 42 public class ScreenSlideActivity extends FragmentActivity { 43 /** 44 * The number of pages (wizard steps) to show in this demo. 45 */ 46 private static final int NUM_PAGES = 5; 47 48 /** 49 * The pager widget, which handles animation and allows swiping horizontally to access previous 50 * and next wizard steps. 51 */ 52 private ViewPager mPager; 53 54 /** 55 * The pager adapter, which provides the pages to the view pager widget. 56 */ 57 private PagerAdapter mPagerAdapter; 58 59 @Override onCreate(Bundle savedInstanceState)60 protected void onCreate(Bundle savedInstanceState) { 61 super.onCreate(savedInstanceState); 62 setContentView(R.layout.activity_screen_slide); 63 64 // Instantiate a ViewPager and a PagerAdapter. 65 mPager = (ViewPager) findViewById(R.id.pager); 66 mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager()); 67 mPager.setAdapter(mPagerAdapter); 68 mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 69 @Override 70 public void onPageSelected(int position) { 71 // When changing pages, reset the action bar actions since they are dependent 72 // on which page is currently active. An alternative approach is to have each 73 // fragment expose actions itself (rather than the activity exposing actions), 74 // but for simplicity, the activity provides the actions in this sample. 75 invalidateOptionsMenu(); 76 } 77 }); 78 } 79 80 @Override onCreateOptionsMenu(Menu menu)81 public boolean onCreateOptionsMenu(Menu menu) { 82 super.onCreateOptionsMenu(menu); 83 getMenuInflater().inflate(R.menu.activity_screen_slide, menu); 84 85 menu.findItem(R.id.action_previous).setEnabled(mPager.getCurrentItem() > 0); 86 87 // Add either a "next" or "finish" button to the action bar, depending on which page 88 // is currently selected. 89 MenuItem item = menu.add(Menu.NONE, R.id.action_next, Menu.NONE, 90 (mPager.getCurrentItem() == mPagerAdapter.getCount() - 1) 91 ? R.string.action_finish 92 : R.string.action_next); 93 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 94 return true; 95 } 96 97 @Override onOptionsItemSelected(MenuItem item)98 public boolean onOptionsItemSelected(MenuItem item) { 99 switch (item.getItemId()) { 100 case android.R.id.home: 101 // Navigate "up" the demo structure to the launchpad activity. 102 // See http://developer.android.com/design/patterns/navigation.html for more. 103 NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class)); 104 return true; 105 106 case R.id.action_previous: 107 // Go to the previous step in the wizard. If there is no previous step, 108 // setCurrentItem will do nothing. 109 mPager.setCurrentItem(mPager.getCurrentItem() - 1); 110 return true; 111 112 case R.id.action_next: 113 // Advance to the next step in the wizard. If there is no next step, setCurrentItem 114 // will do nothing. 115 mPager.setCurrentItem(mPager.getCurrentItem() + 1); 116 return true; 117 } 118 119 return super.onOptionsItemSelected(item); 120 } 121 122 /** 123 * A simple pager adapter that represents 5 {@link ScreenSlidePageFragment} objects, in 124 * sequence. 125 */ 126 private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { ScreenSlidePagerAdapter(FragmentManager fm)127 public ScreenSlidePagerAdapter(FragmentManager fm) { 128 super(fm); 129 } 130 131 @Override getItem(int position)132 public Fragment getItem(int position) { 133 return ScreenSlidePageFragment.create(position); 134 } 135 136 @Override getCount()137 public int getCount() { 138 return NUM_PAGES; 139 } 140 } 141 } 142