本文共 4095 字,大约阅读时间需要 13 分钟。
Screen slides are transitions between one entire screen to another and are common with UIs like setup wizards or slideshows. This lesson shows you how to do screen slides with a provided by the .
s can animate screen slides automatically. Here's what a screen slide looks like that transitions from
If you want to jump ahead and see a full working example, and run the sample app and select the Screen Slide example. See the following files for the code implementation:
src/ScreenSlidePageFragment.java
src/ScreenSlideActivity.java
layout/activity_screen_slide.xml
layout/fragment_screen_slide_page.xml
Create a layout file that you'll later use for the content of a fragment. The following example contains a text view to display some text:
Create a class that returns the layout that you just created in the
method. You can then create instances of this fragment in the parent activity whenever you need a new page to display to the user:
public class ScreenSlidePageFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup rootView = (ViewGroup) inflater.inflate( R.layout.fragment_screen_slide_page, container, false); return rootView; }}
s have built-in swipe gestures to transition through pages, and they display screen slide animations by default, so you don't need to create any.
s use
s as a supply for new pages to display, so the
will use the fragment class that you created earlier.
To begin, create a layout that contains a :
Create an activity that does the following things:
.
abstract class. Implement the
method to supply instances of ScreenSlidePageFragment
as new pages. The pager adapter also requires that you implement the
method, which returns the amount of pages the adapter will create (five in the example).
to the
. public class ScreenSlidePagerActivity extends FragmentActivity { /** * The number of pages (wizard steps) to show in this demo. */ private static final int NUM_PAGES = 5; /** * The pager widget, which handles animation and allows swiping horizontally to access previous * and next wizard steps. */ private ViewPager mPager; /** * The pager adapter, which provides the pages to the view pager widget. */ private PagerAdapter mPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_screen_slide_pager); // Instantiate a ViewPager and a PagerAdapter. mPager = (ViewPager) findViewById(R.id.pager); mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager()); mPager.setAdapter(mPagerAdapter); } @Override public void onBackPressed() { if (mPager.getCurrentItem() == 0) { // If the user is currently looking at the first step, allow the system to handle the // Back button. This calls finish() on this activity and pops the back stack. super.onBackPressed(); } else { // Otherwise, select the previous step. mPager.setCurrentItem(mPager.getCurrentItem() - 1); } } /** * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in * sequence. */ private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { public ScreenSlidePagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return new ScreenSlidePageFragment(); } @Override public int getCount() { return NUM_PAGES; } }}