Using ViewPager for Screen Slides use Screen Slides ViewPager

Source: Internet
Author: User

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 ViewPager provided by the support library. viewPagers can animate screen slides automatically. here's what a screen slide looks like that transitions from

Http://blog.csdn.net/sergeycao
If you want to jump ahead and see a full working example, download 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 the Views
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:

<Com. example. android. animationsdemo. ScrollView
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/content"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">

<TextView style = "? Android: textAppearanceMedium"
Android: padding = "16dp"
Android: lineSpacingMultiplier = "1.2"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/lorem_ipsum"/>

</Com. example. android. animationsdemo. ScrollView>
Create the Fragment
Create a Fragment class that returns the layout that you just created in the onCreateView () 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;
}
}
Screen Slides with ViewPager
ViewPagers 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. viewPagers use PagerAdapters as a supply for new pages to display, so the PagerAdapter will use the fragment class that you created earlier.

To begin, create a layout that contains a ViewPager:

<Android. support. v4.view. ViewPager
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/pager"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"/>
Create an activity that does the following things:

Sets the content view to be the layout with the ViewPager.
Create a class that extends the FragmentStatePagerAdapter abstract class. implement the getItem () method to supply instances of ScreenSlidePageFragment as new pages. the pager adapter also requires that you implement the getCount () method, which returns the amount of pages the adapter will create (five in the example ).
Hooks up the PagerAdapter to the ViewPager.
Handle's the device's back button by moving backwards in the virtual stack of fragments. If the user is already on the first page, go back on the activity back stack.
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
// Back button. This callfinish () 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;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.