Screen Slides References https://developer.android.com/training/animation/screen-slide.html https://developer.android.com/guide/components/fragments.html Overview A fragment can be defined by a class and a layout file. It is used as a modular component in an activity. The activity can dynamically swap out one fragment for another. In this tutorial, you ll learn how to create an activity that initially loads one fragment, and when the user swipes left, replaces the first fragment with the second. To make this happen we ll do the following: 1. Create two fragments. 2. Create a new activity to hold the fragments. Create Two Fragments Create a new fragment by pressing File > New > Fragment > Fragment (Blank). On the Configure Component screen, name the fragment Fragment1 and name the layout fragment1. Uncheck the checkboxes blow the layout name and press Finish. The Android Studio wizard adds a TextView to the fragment1.xml layout file and the necessary code to Fragment1.java. Create a second fragment naming the fragment class Fragment2 and the layout file fragment2. Don t forget to uncheck the checkout boxes.
Once the files are created replace the TextView in fragment2.xml with a button that has its onclick property set to done. DO NOT, however, add a button handler named done to Fragment2.java. Create a New Activity Create a new activity by pressing File > New > Activity > Empty Activity. Name the activity MyActivity and name the layout my_activity. Replace the contents of my_activity.xml with the following code. <?xml version="1.0" encoding="utf-8"?> <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" /> What we re doing here is putting a single widget (a ViewPager) in the activity s layout file. When the activity is loaded, the ViewPager will load (using an adapter) one of the fragments, and when the screen is swiped, the ViewPager will add a different fragment. Next, replace the contents of MyActivity.java with the code below. public class MyActivity extends FragmentActivity { private static final int NUM_PAGES = 2; private ViewPager mpager;
private PagerAdapter mpageradapter; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.my_activity); mpager = (ViewPager) findviewbyid(r.id.pager); mpageradapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); mpager.setadapter(mpageradapter); //optional: set to second page (starts at 0) mpager.setcurrentitem(1); public void onbackpressed() { if (mpager.getcurrentitem() == 0) { super.onbackpressed(); else { mpager.setcurrentitem(mpager.getcurrentitem() - 1);
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { public ScreenSlidePagerAdapter(FragmentManager fm) { super(fm); public Fragment getitem(int position) { if (position == 0) return new Fragment1(); else return new Fragment2(); public int getcount() { return NUM_PAGES; Notice that MyActivity extends FragmentActivity rather than AppCompatActivity. Notice too that we create an inner class named ScreenSlidePagerAdapter. An instance of this class is registered as the adapter (a data source) for the ViewPager. When the ViewPager needs a different fragment, the adapter serves it up by calling getitem().
Test the Code Create a button in one of your accessible activities so that when it is pressed MyActivity is loaded. When pressed, you should see fragment 1 initially displayed on the screen. When you swipe left, you should see the contents of Fragment2 (the button) come in from the right. At this point, the button should not work. Adding a Button Handler for the Done Button Recall that when you see fragment 1 or the done button on the screen, you re still actually viewing the MyAcitivity activity. If a button is displayed in an activity, regardless if it was placed there by a fragment or not, the button s onclick handler must be defined in the activity s class file. Add an onclick handler named done in MyActivity.java. Have the body of the function simply call finish(). When you run the app and press the done button, the app should return you to the activity that started MyActivity.