Android FragmentStatePagerAdapter Example

来源:互联网 发布:学生开淘宝店怎么样 编辑:程序博客网 时间:2024/05/16 09:21

Android development

is full of surprises, you can never predict what’s coming your way. In my latest assignment atTruiton I had to develop an app in which I user can fill segmented data in an interactive way. The best approach I could think of was screen slides. Now to solve this, I used AndroidFragmentStatePagerAdapter and ViewPager class. And so I thought of writing this Android FragmentStatePagerAdapter Example tutorial. This way each screen can act as a segment and data can be filled in all segments by sliding from one screen to another.

Although there are two ways to implement Android ViewPager class, but in this tutorial Iam only going to discuss Android FragmentStatePagerAdapter class with an example. Since the main purpose/advantage of AndroidFragmentStatePagerAdapter class is to it consume less memory as compared to its counterpart, I used this to solve my problem. Android FragmentStatePagerAdapter class consumes less memory, because it destroys fragments, as soon as they are not visible to user, keeping only saved state of that fragment. And savedInstanceState is all, what I needed in my app to restore the filled out data by user.

But there may exist a situation where you would like to store the entire fragment in memory, if that’s the case have look at my complete series onPagerAdapters used to implement ViewPager class:

  1. Android FragmentStatePagerAdapter
  2. Android FragmentPagerAdapter
  3. Android FragmentPagerAdapter vs FragmentStatePagerAdapter

Now since the Fragments were introduced in android API 11, earlier versions of android are not capable of running fragments and swipes, unless a support library is used in app. Therefore I built this app with Android support library v4, package nameandroid.support.v4. As a result all versions will run following AndroidFragmentStatePagerAdapter Example.

Android FragmentStatePagerAdapter Example

To get started, first thing to do is, to create a layout for ViewPager and AndroidFragmentStatePagerAdapter class. Now while creating a layout please keep in mind to use android.support.v4.view.ViewPager as we would like our app to be compatible with older versions of android. Have a look atfragment_pager.xml.

The layout above is the main layout where all the fragments would be inflated and displayed. Todisplay sliding views, now we need to declare a main class where Android FragmentStatePagerAdapter class would be declared. The class defined below isMainActivity.java.

In the class above I defined my own FragmentStatePagerAdapter by the name of MyAdapter class. In this adapter the main method isgetItem. The getItem method in FragmentStatePagerAdapter calls each fragment when user swipes though them. In thisMyAdapter class we are calling two Fragments, ImageFragment andArrayListFragment. lets have a look at ImageFragment class first.

Here in the class above I have defined a static method by the name of init. This method takes the position as argumentval from inherited/extended Android FragmentStatePagerAdapter class and further its used in onCreate and onCreateView method of this Fragment accordingly. ImageFragment usesfragment_image layout, so lets have a look at fragment_image.xml.

Next lets have a look at the ArrayListFragment.java.

The functioning of this ListFragment is also same as of ImageFragment, it has aninit method which takes argument from FragmentStatePagerAdapter, the only difference is that it displays a list. Have a look at its layoutfragment_pager_list.xml.

As of now we have our Android FragmentStatePagerAdapter and ViewPager setup and working, so your project should look something like this.

With this I can conclude this tutorial on Android FragmentStatePagerAdapter and ViewPager. In this tutorial I created an app which had the functionality todisplay 10 sliding fragments, or swipe views. Also I defined two types of fragments, an ImageFragment andListFragment, which were used according to the required position in FragmentStatePagerAdapter.

As I mentioned earlier Android FragmentStatePagerAdapter destroys fragments as soon as, they are not visible to user. If you are developing an app which has less number of fragments and you want to keep them in memory, have look at my tutorial onAndroid FragmentPagerAdapter. If this helped you please like and share this with your friends on Google+ and Facebook. Also like our Facebookpage for updates.



http://www.truiton.com/2013/05/android-fragmentstatepageradapter-example/

0 0
原创粉丝点击