<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PagerAdapter Archives - Truiton</title>
	<atom:link href="https://www.truiton.com/tag/pageradapter/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Technology Reaching Us In Time - Online</description>
	<lastBuildDate>Mon, 25 Nov 2019 12:04:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://i0.wp.com/www.truiton.com/wp-content/uploads/2022/11/cropped-truiton_new_logo_half_white.png?fit=32%2C32&#038;ssl=1</url>
	<title>PagerAdapter Archives - Truiton</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">54534495</site>	<item>
		<title>Android FragmentPagerAdapter vs FragmentStatePagerAdapter</title>
		<link>https://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/</link>
					<comments>https://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Tue, 04 Jun 2013 19:35:33 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[PagerAdapter]]></category>
		<category><![CDATA[Screen Slides]]></category>
		<category><![CDATA[sliding]]></category>
		<category><![CDATA[ViewPager]]></category>
		<guid isPermaLink="false">http://blog.truiton.com/?p=348</guid>

					<description><![CDATA[<p>To develop an app in Android with sliding views, a variation of PagerAdapter class along with ViewPager class is used. Recently I wrote two tutorials about PagerAdapter classes, describing Android FragmentPagerAdapter and FragmentStatePagerAdapter, as both of them have benefits of their own. This Android FragmentPagerAdapter vs FragmentStatePagerAdapter is my concluding article on PagerAdapter series. In&#8230;&#160;<a href="https://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android FragmentPagerAdapter vs FragmentStatePagerAdapter</span></a></p>
<p>The post <a href="https://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/">Android FragmentPagerAdapter vs FragmentStatePagerAdapter</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="640" height="480" data-attachment-id="350" data-permalink="https://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/a-little-update/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/06/2136923757_233732c650_z.jpg?fit=640%2C480&amp;ssl=1" data-orig-size="640,480" data-comments-opened="1" data-image-title="&amp;#8220;a little update&amp;#8221;" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/06/2136923757_233732c650_z.jpg?fit=640%2C480&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/06/2136923757_233732c650_z.jpg?resize=640%2C480" alt="Android FragmentPagerAdapter vs FragmentStatePagerAdapter" class="wp-image-350" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/06/2136923757_233732c650_z.jpg?w=640&amp;ssl=1 640w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/06/2136923757_233732c650_z.jpg?resize=300%2C225&amp;ssl=1 300w" sizes="(max-width: 640px) 100vw, 640px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">To develop an app in Android with sliding views, a variation of <a title="PagerAdapter" href="http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html" target="_blank" rel="noopener noreferrer">PagerAdapter</a> class along with <a title="ViewPager" href="http://developer.android.com/reference/android/support/v4/view/ViewPager.html" target="_blank" rel="noopener noreferrer">ViewPager</a> class is used. Recently I wrote two tutorials about <em>PagerAdapter</em> classes, describing Android FragmentPagerAdapter and FragmentStatePagerAdapter, as both of them have benefits of their own. This <a title="Android FragmentPagerAdapter vs FragmentStatePagerAdapter" href="http://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/" target="_blank" rel="noopener noreferrer">Android FragmentPagerAdapter vs FragmentStatePagerAdapter</a> is my concluding article on <em>PagerAdapter</em> series. In this article I will compare both <em>PagerAdapter</em>s and explain the perfect situation for their use. If you wish to read my complete series on <em>PagerAdapter</em>s and <em>ViewPager</em>s have a look at links below:</p>



<ol class="wp-block-list"><li><a title="Android FragmentStatePagerAdapter Example" href="http://www.truiton.com/2013/05/android-fragmentstatepageradapter-example/" target="_blank" rel="noopener noreferrer">Android FragmentStatePagerAdapter</a></li><li><a title="Android FragmentPagerAdapter Example" href="http://www.truiton.com/2013/05/android-fragmentpageradapter-example/" target="_blank" rel="noopener noreferrer">Android FragmentPagerAdapter</a></li><li><a title="Android FragmentPagerAdapter vs FragmentStatePagerAdapter" href="http://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/" target="_blank" rel="noopener noreferrer">Android FragmentPagerAdapter vs FragmentStatePagerAdapter</a></li></ol>



<p class="wp-block-paragraph">To compare&nbsp;Android FragmentPagerAdapter vs FragmentStatePagerAdapter, first lets understand what Android <em>FragmentPagerAdapter</em> does. <em>FragmentPagerAdapter</em> is used along with <em>ViewPager</em> class, to display sliding fragments. <em>ViewPager</em> class is a sort of layout manager which provides the functionality of flipping data in form of pages, left and right. In simple words <em>ViewPager</em> class provides the functionality to flip pages in an app. Moving forward with this basic info, to understand the&nbsp;Android FragmentPagerAdapter vs FragmentStatePagerAdapter lets try to understand the technical aspect of&nbsp;Android <em>FragmentPagerAdapter</em>.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Android <em>FragmentPagerAdapter</em> is type of <em>PagerAdapter</em> which populates the data inside the <em>ViewPager,</em> which in turn allows the flipping of data in form of pages. Usually to display sliding fragments two methods of <em>FragmentPagerAdapter</em> are used, <em>getCount</em> and <em>getItem</em>. The first one returns the number of fragments to be displayed, and the second one is used to display the actual fragment. To understand the code have a look at my tutorial about <a title="Android FragmentPagerAdapter Example" href="http://www.truiton.com/2013/05/android-fragmentpageradapter-example/" target="_blank" rel="noopener noreferrer">Android FragmentPagerAdapter</a>. The purpose for using this type of fragment adapter is to keep the whole fragment in memory. Its view hierarchy may be destroyed but the fragment may still remain in memory. Hence it is advised to use this Android <em>FragmentPagerAdapter</em> only when there are low number of static fragments, since all fragments would be kept in the memory and this would increase the memory heap. Which could result a slow running app.</p>



<p class="wp-block-paragraph">Further to understand Android FragmentPagerAdapter vs FragmentStatePagerAdapter, lets try to understand the <em>FragmentStatePagerAdapter</em>. This adapter also falls under the <em>PagerAdapter</em> class and is used with <em>ViewPager</em> to display sliding views. It also has same methods <em>getCount</em> and <em>getItem</em>. Which are used to get the fragment count and get the fragment reference respectively, same as in <em>FragmentPagerAdapter</em>. To further understand the code part of it please read my tutorial on <a title="Android FragmentStatePagerAdapter Example" href="http://www.truiton.com/2013/05/android-fragmentstatepageradapter-example/" target="_blank" rel="noopener noreferrer">Android FragmentStatePagerAdapter</a>. The main functionality for which <em>FragmentStatePagerAdapter </em>is used, to accommodate a large number of fragments in <em>ViewPager</em>. As this adapter destroys the fragment when it is not visible to the user and only <em>savedInstanceState</em> of the fragment is kept for further use. This way a low amount of memory is used and a better performance is delivered in case of dynamic fragments.</p>



<h2 class="wp-block-heading">Android FragmentPagerAdapter vs FragmentStatePagerAdapter</h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Now that I have explained what <em>FragmentStatePagerAdapter</em> and <em>FragmentPagerAdapter</em> does, lets finally discuss <a title="Android FragmentPagerAdapter vs FragmentStatePagerAdapter" href="http://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/" target="_blank" rel="noopener noreferrer">Android FragmentPagerAdapter vs FragmentStatePagerAdapter</a>. As I explained <em>FragmentPagerAdapter</em> stores the whole fragment in memory, and could increase a memory overhead if a large amount of fragments are used in ViewPager. In contrary its sibling, <em>FragmentStatePagerAdapter</em> only stores the <em>savedInstanceState</em> of fragments, and destroys all the fragments when they lose focus. Therefore <em>FragmentStatePagerAdapter</em> should be used when we have to use dynamic fragments, like fragments with widgets, as their data could be stored in the <em>savedInstanceState</em>. Also it wont affect the performance even if there are large number of fragments. In contrary its sibling <em>FragmentPagerAdapter</em> should be used when we need to store the whole fragment in memory. When I say the whole fragment is kept in memory it means, its instances wont be destroyed and would create a memory overhead. Therefore it is advised to use <em>FragmentPagerAdapter</em> only when there are low number of fragments for <em>ViewPager</em>. It would be even better if the fragments are static, since they would not be having large amount of objects whose instances would be stored. Hope this clears out the difference between Android <em>FragmentPagerAdapter</em> and <em>FragmentStatePagerAdapter</em>. If you like this article please share it with your friends on Google+ and Facebook, also like our page on Facebook for latest updates.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/">Android FragmentPagerAdapter vs FragmentStatePagerAdapter</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">348</post-id>	</item>
		<item>
		<title>Android FragmentPagerAdapter Example</title>
		<link>https://www.truiton.com/2013/05/android-fragmentpageradapter-example/</link>
					<comments>https://www.truiton.com/2013/05/android-fragmentpageradapter-example/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Fri, 31 May 2013 12:04:21 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[PagerAdapter]]></category>
		<category><![CDATA[Screen Slides]]></category>
		<category><![CDATA[sliding]]></category>
		<category><![CDATA[ViewPager]]></category>
		<guid isPermaLink="false">http://blog.truiton.com/?p=332</guid>

					<description><![CDATA[<p>How cool is it to have an app which has swipe views, it makes app interactive and fun to use, right? So being a mobile app consultant at Truiton I was working on an app where segmented forms were to be filled by user. I thought why not use swipe tabs and sliding views. I&#8230;&#160;<a href="https://www.truiton.com/2013/05/android-fragmentpageradapter-example/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android FragmentPagerAdapter Example</span></a></p>
<p>The post <a href="https://www.truiton.com/2013/05/android-fragmentpageradapter-example/">Android FragmentPagerAdapter Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" decoding="async" width="640" height="427" data-attachment-id="338" data-permalink="https://www.truiton.com/2013/05/android-fragmentpageradapter-example/go-by-streetcar-part-iii/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/6625368289_37d9368225_z.jpg?fit=640%2C427&amp;ssl=1" data-orig-size="640,427" data-comments-opened="1" data-image-title="Go By Streetcar Part III" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/6625368289_37d9368225_z.jpg?fit=640%2C427&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/6625368289_37d9368225_z.jpg?resize=640%2C427" alt="Go By Streetcar Part III" class="wp-image-338" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/6625368289_37d9368225_z.jpg?w=640&amp;ssl=1 640w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/6625368289_37d9368225_z.jpg?resize=300%2C200&amp;ssl=1 300w" sizes="(max-width: 640px) 100vw, 640px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">How cool is it to have an app which has swipe views, it makes app interactive and fun to use, right? So being a mobile app consultant at <a title="Truiton" href="http://www.truiton.com" target="_blank" rel="noopener noreferrer">Truiton</a> I was working on an app where segmented forms were to be filled by user. I thought why not use swipe tabs and sliding views. I searched and found this can be implemented by Android ViewPager class, which is used in conjunction with Android <a title="FragmentPagerAdapter" href="http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html" target="_blank" rel="noopener noreferrer">FragmentPagerAdapter</a> class or&nbsp;Android <a title="FragmentStatePagerAdapter" href="http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html" target="_blank" rel="noopener noreferrer">FragmentStatePagerAdapter</a> class. Since any sort of software development requires precision and accuracy in work my purpose was solved by the later one. Still I decided to write a tutorial about&nbsp;<a title="Android FragmentPagerAdapter Example" href="http://www.truiton.com/2013/05/android-fragmentpageradapter-example/" target="_blank" rel="noopener noreferrer">Android FragmentPagerAdapter Example</a>.</p>



<p class="wp-block-paragraph">There are two ways to implement Android ViewPager class, but this tutorial only focuses on Android FragmentPagerAdapter with an example. Now since both adapters have different ways of working, you might want to read about the other one too, have a look at my series on <em>PagerAdapters</em> :</p>



<ol class="wp-block-list"><li><a title="Android FragmentStatePagerAdapter Example" href="http://www.truiton.com/2013/05/android-fragmentstatepageradapter-example/" target="_blank" rel="noopener noreferrer">Android FragmentStatePagerAdapter</a></li><li><a title="Android FragmentPagerAdapter Example" href="http://www.truiton.com/2013/05/android-fragmentpageradapter-example/" target="_blank" rel="noopener noreferrer">Android FragmentPagerAdapter</a></li><li><a title="Android FragmentPagerAdapter vs FragmentStatePagerAdapter" href="http://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/" target="_blank" rel="noopener noreferrer">Android FragmentPagerAdapter vs FragmentStatePagerAdapter</a></li></ol>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">In my previous tutorial I mentioned a case, if we wish to keep the fragment in the memory. Here steps in <em>Android FragmentPagerAdapter</em>, the main advantage of using Android <em>FragmentPagerAdapter</em> is that, it keeps the fragment in memory. This definitely increases the memory usage, therefore it is advised to use Android <em>FragmentPagerAdapter</em> only when there are less number of fragments. If you app has non changing fragments like, fragments with images or static text it might work even better. But using Android <em>FragmentPagerAdapter</em> for large number of fragments would result choppy and laggy UX.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android FragmentPagerAdapter Example</h2>



<p class="wp-block-paragraph">Lets start with the main activity, this activity is where the Android <em>FragmentPagerAdapter</em> class is declared. Have a look at <strong>MainActivity.java</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.fragmentpageradapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends FragmentActivity {
 static final int ITEMS = 10;
 MyAdapter mAdapter;
 ViewPager mPager;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.fragment_pager);
 mAdapter = new MyAdapter(getSupportFragmentManager());
 mPager = (ViewPager) findViewById(R.id.pager);
 mPager.setAdapter(mAdapter);

 Button button = (Button) findViewById(R.id.first);
 button.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
 mPager.setCurrentItem(0);
 }
 });
 button = (Button) findViewById(R.id.last);
 button.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
 mPager.setCurrentItem(ITEMS - 1);
 }
 });
 }

 public static class MyAdapter extends FragmentPagerAdapter {
 public MyAdapter(FragmentManager fragmentManager) {
 super(fragmentManager);
 }

 @Override
 public int getCount() {
 return ITEMS;
 }

 @Override
 public Fragment getItem(int position) {
 switch (position) {
 case 0: // Fragment # 0 - This will show image
 return ImageFragment.init(position);
 case 1: // Fragment # 1 - This will show image
 return ImageFragment.init(position);
 default:// Fragment # 2-9 - Will show list
 return TruitonListFragment.init(position);
 }
 }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }

}</pre>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">In the above class one can see that <em>MainActivity</em> extends <em>FragmentActivity</em> and in this class, both <em>MyAdapter</em> and <em>ViewPager</em> class are instantiated. If you look closely you would find that <em>MyAdapter</em> class extends Android <em>FragmentPagerAdapter</em> class. Lets have a look at the extended Android <em>FragmentPagerAdapter</em> class, here exists a method by the name of <em>getItem</em>. This <em>getItem</em> method is the one which calls all the fragments which are to be displayed according to the position. MainActivity.java is displayed by layout <strong>fragment_pager.xml</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="4dip" >

    &lt;android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" >
    &lt;/android.support.v4.view.ViewPager>

    &lt;LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="center"
        android:measureWithLargestChild="true"
        android:orientation="horizontal" >

        &lt;Button
            android:id="@+id/first"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="First" >
        &lt;/Button>

        &lt;Button
            android:id="@+id/last"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Last" >
        &lt;/Button>
    &lt;/LinearLayout>

&lt;/LinearLayout></pre>



<p class="wp-block-paragraph">Above is the main layout where all fragments would be displayed. Have a look at <em>android.support.v4.view.ViewPager</em> tag, since this tag is used here, this app would run on android devices lower than API version 11 too. Next lets have a look at <strong>ImageFragment.java</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.fragmentpageradapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ImageFragment extends Fragment {
	int fragVal;

	static ImageFragment init(int val) {
		ImageFragment truitonFrag = new ImageFragment();
		// Supply val input as an argument.
		Bundle args = new Bundle();
		args.putInt("val", val);
		truitonFrag.setArguments(args);
		return truitonFrag;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View layoutView = inflater.inflate(R.layout.fragment_image, container,
				false);
		View tv = layoutView.findViewById(R.id.text);
		((TextView) tv).setText("Truiton Fragment #" + fragVal);
		return layoutView;
	}
}</pre>



<p class="wp-block-paragraph">In the above class a <span style="text-decoration: underline;">static</span> method by the name of <em>init</em> is declared. This <em>init</em> method is called in the extended Android <em>FragmentPagerAdapter</em> class with position of the fragment as argument. This argument is later on used in onCreate and onCreateView method of this ImageFragment. The layout file for this class is <strong>fragment_image.xml</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    &lt;TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/hello_world"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    &lt;ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="36dp"
        android:src="@drawable/truiton" />

&lt;/RelativeLayout></pre>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Now have a look at the second type of fragment used in Android FragmentPagerAdapter Example, <strong>TruitonListFragment.java</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.fragmentpageradapter;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class TruitonListFragment extends ListFragment {
 int fragNum;
 String arr[] = { "This is", "a Truiton", "Demo", "App", "For", "Showing",
 "FragmentPagerAdapter", "and ViewPager", "Implementation" };

 static TruitonListFragment init(int val) {
 TruitonListFragment truitonList = new TruitonListFragment();

 // Supply val input as an argument.
 Bundle args = new Bundle();
 args.putInt("val", val);
 truitonList.setArguments(args);

 return truitonList;
 }

 /**
  * Retrieving this instance's number from its arguments.
  */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 fragNum = getArguments() != null ? getArguments().getInt("val") : 1;
 }

 /**
  * The Fragment's UI is a simple text view showing its instance number and
  * an associated list.
  */
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
 View layoutView = inflater.inflate(R.layout.fragment_pager_list,
 container, false);
 View tv = layoutView.findViewById(R.id.text);
 ((TextView) tv).setText("Truiton Fragment #" + fragNum);
 return layoutView;
 }

 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
 super.onActivityCreated(savedInstanceState);
 setListAdapter(new ArrayAdapter&lt;String>(getActivity(),
 android.R.layout.simple_list_item_1, arr));
 }

 @Override
 public void onListItemClick(ListView l, View v, int position, long id) {
 Log.i("Truiton FragmentList", "Item clicked: " + id);
 }
}</pre>



<p class="wp-block-paragraph">In the above class, the only difference is, that ListFragment is used, rest is same as in ImageFragment. It has a <span style="text-decoration: underline;">static</span> <em>init</em> method which is called from extended Android <em>FragmentPagerAdapter</em>. It uses layout <strong>fragment_pager_list.xml</strong> :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:drawable/gallery_thumb"
    android:orientation="vertical" >

    &lt;TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/hello_world"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    &lt;FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        &lt;ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false" />
    &lt;/FrameLayout>

&lt;/LinearLayout></pre>



<p class="wp-block-paragraph">After defining all the classes and layouts your app should look like screenshots below</p>



<ul data-carousel-extra='{&quot;blog_id&quot;:1,&quot;permalink&quot;:&quot;https://www.truiton.com/2013/05/android-fragmentpageradapter-example/&quot;}'  class="wp-block-gallery columns-3 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex"><li class="blocks-gallery-item"><figure><a href="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" width="353" height="627" data-attachment-id="335" data-permalink="https://www.truiton.com/2013/05/android-fragmentpageradapter-example/fragmentpageradapter/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter.png?fit=353%2C627&amp;ssl=1" data-orig-size="353,627" data-comments-opened="1" data-image-title="" data-image-description="" data-image-caption="&lt;p&gt;Android FragmentPagerAdapter ImageFragment&lt;/p&gt;
" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter.png?fit=353%2C627&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter.png?resize=353%2C627&#038;ssl=1" alt="Android FragmentPagerAdapter" data-id="335" class="wp-image-335"/></a></figure></li><li class="blocks-gallery-item"><figure><a href="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter2.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" width="349" height="628" data-attachment-id="336" data-permalink="https://www.truiton.com/2013/05/android-fragmentpageradapter-example/fragmentpageradapter2/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter2.png?fit=349%2C628&amp;ssl=1" data-orig-size="349,628" data-comments-opened="1" data-image-title="" data-image-description="" data-image-caption="&lt;p&gt;Android FragmentPagerAdapter Sliding View&lt;/p&gt;
" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter2.png?fit=349%2C628&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter2.png?resize=349%2C628&#038;ssl=1" alt="Android FragmentPagerAdapter" data-id="336" class="wp-image-336" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter2.png?w=349&amp;ssl=1 349w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter2.png?resize=166%2C300&amp;ssl=1 166w" sizes="auto, (max-width: 349px) 100vw, 349px" /></a></figure></li><li class="blocks-gallery-item"><figure><a href="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter3.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" width="353" height="630" data-attachment-id="337" data-permalink="https://www.truiton.com/2013/05/android-fragmentpageradapter-example/fragmentpageradapter3/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter3.png?fit=353%2C630&amp;ssl=1" data-orig-size="353,630" data-comments-opened="1" data-image-title="" data-image-description="" data-image-caption="&lt;p&gt;Android FragmentPagerAdapter TruitonListFragment&lt;/p&gt;
" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter3.png?fit=353%2C630&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter3.png?resize=353%2C630&#038;ssl=1" alt="Android FragmentPagerAdapter" data-id="337" class="wp-image-337" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter3.png?w=353&amp;ssl=1 353w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/05/FragmentPagerAdapter3.png?resize=168%2C300&amp;ssl=1 168w" sizes="auto, (max-width: 353px) 100vw, 353px" /></a></figure></li></ul>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">In this tutorial I defined a class which extended <em>FragmentActivity</em>, and instantiated extended version of Android <em>FragmentPagerAdapter</em> and <em>ViewPager</em>. This whole <a title="Android FragmentPagerAdapter Example" href="http://www.truiton.com/2013/05/android-fragmentpageradapter-example/">Android <em>FragmentPagerAdapter</em> example</a> is made with support library, hence this would run on all android devices. Again to sum up, I would say that use Android <em>FragmentPagerAdapter</em> only when you have low amount of fragments that you want to keep in memory. Otherwise it will create a memory overhead and affect your app&#8217;s performance. This would work even better with non changing fragments. If you still have concerns on which PagerAdapter to use, have a look at <a title="Android FragmentPagerAdapter vs FragmentStatePagerAdapter" href="http://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/" target="_blank" rel="noopener noreferrer">Android FragmentPagerAdapter vs FragmentStatePagerAdapter</a>. Hope this helped you out, if it did please like and share this with your friends on Google+ and Facebook. Also like our Facebook page for updates.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2013/05/android-fragmentpageradapter-example/">Android FragmentPagerAdapter Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2013/05/android-fragmentpageradapter-example/feed/</wfw:commentRss>
			<slash:comments>19</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">332</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: www.truiton.com @ 2026-07-01 11:06:52 by W3 Total Cache
-->