Site icon C1CTech

Android Sliding Views Using ViewPager With PagerAdapter,FragmentPagerAdapter

<p>In this tutorial we are going to learn about how to create swipe views in android application&period; We can create swipe view in android application using the <a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v4&sol;view&sol;ViewPager"><span style&equals;"color&colon; &num;008000&semi;"><strong>ViewPager<&sol;strong><&sol;span><&sol;a> widget which is available in the support library&period; ViewPager is a layout widget which is capable of holding many child views and each child view represent a separate page&period; To insert child views that represent each page we need to hook up the viewpager to a <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v4&sol;view&sol;PagerAdapter">pagerAdapter<&sol;a>&period;<&sol;strong><&sol;span> You have two options for the viewpager adapter &colon;<&sol;p>&NewLine;<p><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v4&sol;app&sol;FragmentPagerAdapter"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>FragmentPagerAdapter<&sol;strong><&sol;span><&sol;a> &colon; This is the best option if there is only a limited number of pages&lpar;Fragment&rpar; to show&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v4&sol;app&sol;FragmentStatePagerAdapter">FragmentStatePagerAdapter<&sol;a><&sol;span> <&sol;strong><&sol;span>&colon; You can use this adapter if there is large number of pages or the number of pages are indeterminate&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"z3aTddBdcTw" title&equals;"Android ViewPager Example"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;z3aTddBdcTw"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;z3aTddBdcTw&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android ViewPager Example"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>Here In this tutorial by creating a simple example I will demonstrate you how to swipe between the pages using <span style&equals;"color&colon; &num;008000&semi;"><strong>PagerAdapter <&sol;strong><&sol;span>and how to swipe between the fragments using <span style&equals;"color&colon; &num;008000&semi;"><strong>FragmentPagerAdapter<&sol;strong><&sol;span> &period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Create new project<&sol;span><&sol;strong><&sol;h3>&NewLine;<p><strong>1<&sol;strong>&period; Create a new project in <strong><span style&equals;"color&colon; &num;008000&semi;">Android<&sol;span> <span style&equals;"color&colon; &num;008000&semi;">Studio<&sol;span><&sol;strong> from <span style&equals;"color&colon; &num;008000&semi;"><strong>File &Implies; New Project<&sol;strong><&sol;span> by filling the required details&period; When it prompts you to select the activity&comma; choose <span style&equals;"color&colon; &num;008000&semi;"><strong>Empty Activity<&sol;strong><&sol;span> and continue&period;<&sol;p>&NewLine;<p><strong>2<&sol;strong>&period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span> located in app level and add dependency <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&&num;8216&semi;com&period;android&period;support&colon;design&colon;27&period;1&period;1&&num;8217&semi;<&sol;strong><&sol;span> &period; It is needed for <span style&equals;"color&colon; &num;008000&semi;"><strong>tabLayout <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">widget<&sol;span><&sol;span> &period;<&sol;p>&NewLine;<pre><code>dependencies &lbrace;&NewLine; &NewLine; implementation 'com&period;android&period;support&colon;appcompat-v7&colon;27&period;1&period;1'&NewLine; implementation 'com&period;android&period;support&period;constraint&colon;constraint-layout&colon;1&period;1&period;2'&NewLine; implementation 'com&period;android&period;support&colon;support-v4&colon;27&period;1&period;1'&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Add this <&sol;span> <&sol;strong> &NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"> <strong> compile 'com&period;android&period;support&colon;design&colon;27&period;1&period;1'<&sol;strong><&sol;span>&NewLine; &NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p><strong>3<&sol;strong>&period; Create package <strong><span style&equals;"color&colon; &num;0000ff&semi;">fragment<&sol;span><&sol;strong> contains two fragment <strong><span style&equals;"color&colon; &num;008000&semi;">Yogasan&period;java<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;008000&semi;">SuryaNamaskar&period;java<&sol;span><&sol;strong> &period;<&sol;p>&NewLine;<p><strong>4<&sol;strong>&period; Both fragment will contain pages which we will swipe using viewpager with <strong><span style&equals;"color&colon; &num;008000&semi;">pagerAdapter<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Using PagerAdapter<&sol;span><&sol;strong><&sol;h4>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>PagerAdapter<&sol;strong><&sol;span> allow us to populate pages inside of a viewpager&period;<&sol;p>&NewLine;<p>When you implement a PagerAdapter&comma; you must override the following methods at minimum&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><a href&equals;"http&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v4&sol;view&sol;PagerAdapter&period;html&num;getCount&lpar;&rpar;">getCount&lpar;&rpar;<&sol;a><&sol;strong> – Return the number of views available&period;&comma; i&period;e&period;&comma; number of pages to be displayed&sol;created in the ViewPager&period;<&sol;li>&NewLine;<li><strong><a href&equals;"http&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v4&sol;view&sol;PagerAdapter&period;html&num;instantiateItem&lpar;android&period;view&period;ViewGroup&comma;&percnt;20int&rpar;">instantiateItem&lpar;&rpar;<&sol;a> <&sol;strong>– This method will create the page for the given position passed to it as an argument&period; In our case&comma; we inflate our layout resource which contains one imageView for image and one textView which will contain image related description&period; Finally&comma; the inflated view is added to the container &lpar; ViewPager&rpar; and return it as well&period;<&sol;li>&NewLine;<li><strong><a href&equals;"http&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v4&sol;view&sol;PagerAdapter&period;html&num;destroyItem&lpar;android&period;view&period;ViewGroup&comma;&percnt;20int&comma;&percnt;20java&period;lang&period;Object&rpar;">destroyItem&lpar;&rpar;<&sol;a> <&sol;strong>– Removes the page from the container for the given position&period; We simply removed object using <a href&equals;"http&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;view&sol;ViewGroup&period;html&num;removeView&lpar;android&period;view&period;View&rpar;"><strong>removeView<&sol;strong>&lpar;&rpar;<&sol;a> but could’ve also used <a href&equals;"http&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;view&sol;ViewGroup&period;html&num;removeViewAt&lpar;int&rpar;"><strong>removeViewAt<&sol;strong>&lpar;&rpar;<&sol;a> by passing it the position&period;<&sol;li>&NewLine;<li><strong><a href&equals;"http&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v4&sol;view&sol;PagerAdapter&period;html&num;isViewFromObject&lpar;android&period;view&period;View&comma;&percnt;20java&period;lang&period;Object&rpar;">isViewFromObject&lpar;&rpar;<&sol;a> <&sol;strong>– ViewPager associates each page with a key Object instead of working with Views directly&period; This key is used to track and uniquely identify a given page independent of its position in the adapter&period; This method checks whether the View passed to it &lpar;representing the page&rpar; is associated with that key object&lpar;returned by<strong> <a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v4&sol;view&sol;PagerAdapter&period;html&num;instantiateItem&lpar;android&period;view&period;ViewGroup&comma;&percnt;20int&rpar;">instantiateItem&lpar;ViewGroup&comma; int&rpar;<&sol;a><&sol;strong>&rpar; or not&period; It is required by a PagerAdapter to function properly&period; It returns true if view is associated with the key object &period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>As we swipe the page the pageAdapter firstly destroys the page previous to the one from which it is coming from by calling destroyItem&lpar;&rpar; and creates the page next to it by calling instantiateItem&lpar;&rpar; &period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">For example&colon;<&sol;span><&sol;strong> we have four pages&lpar;page1&comma;page2&comma;page3&comma;page4&rpar;&period;Suppose currently i am on page2 now as i swipe page2 to move to page3 &comma;destroyItem&lpar;&rpar; get called  first and destroy the page1&lpar;View&rpar; and then instantiateItem&lpar;&rpar; get called and a page next to page3 i&period;e page4 get created&period;This is how <strong><span style&equals;"color&colon; &num;008000&semi;">pageAdapter<&sol;span><&sol;strong> works&period;<&sol;p>&NewLine;<p><strong>5<&sol;strong>&period; Here’s how <strong><span style&equals;"color&colon; &num;008000&semi;">res&sol;layout&sol;surya&lowbar;namaskar&lowbar;content&period;xml<&sol;span><&sol;strong> will look like which is inflated in the <span style&equals;"color&colon; &num;008000&semi;"><strong>instantiateItem&lpar;&rpar;<&sol;strong><&sol;span> method&colon;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">surya&lowbar;namaskar&lowbar;content&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&gt&semi;&NewLine;&NewLine; &lt&semi;ScrollView&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&gt&semi;&NewLine;&NewLine; &lt&semi;RelativeLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&gt&semi;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;imageView"&NewLine; android&colon;layout&lowbar;width&equals;"190dp"&NewLine; android&colon;layout&lowbar;height&equals;"190dp"&NewLine; android&colon;layout&lowbar;alignParentTop&equals;"true"&NewLine; android&colon;layout&lowbar;centerInParent&equals;"true" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;textView"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;below&equals;"&commat;&plus;id&sol;imageView"&NewLine; android&colon;paddingLeft&equals;"8dp"&NewLine; android&colon;paddingRight&equals;"5dp"&NewLine; android&colon;textSize&equals;"12dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"10dp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine; &lt&semi;&sol;RelativeLayout&gt&semi;&NewLine; &lt&semi;&sol;ScrollView&gt&semi;&NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;code><&sol;pre>&NewLine;<p><strong>6<&sol;strong>&period; Open <strong><span style&equals;"color&colon; &num;008000&semi;">Yogasan&period;java<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;008000&semi;"><strong>SuryaNamaskar&period;java<&sol;strong><&sol;span> and write the below code&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Yogasan&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre><code>package com&period;c1ctech&period;androidviewpagerexample&period;fragment&semi;&NewLine;&NewLine;import android&period;content&period;Context&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v4&period;app&period;Fragment&semi;&NewLine;import android&period;support&period;v4&period;app&period;FragmentActivity&semi;&NewLine;import android&period;support&period;v4&period;view&period;PagerAdapter&semi;&NewLine;import android&period;support&period;v4&period;view&period;ViewPager&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;view&period;ViewGroup&semi;&NewLine;import android&period;widget&period;ImageView&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;&NewLine;import com&period;c1ctech&period;androidviewpagerexample&period;R&semi;&NewLine;&NewLine;&NewLine;public class Yogasan extends Fragment &lbrace;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public View onCreateView&lpar;LayoutInflater inflater&comma; ViewGroup container&comma; Bundle savedInstanceState&rpar; &lbrace;&NewLine;&NewLine; View view&equals; inflater&period;inflate&lpar;R&period;layout&period;fragment&lowbar;yogasan&comma; container&comma; false&rpar;&semi;&NewLine; &lpar;&lpar;ViewPager&rpar; view&period;findViewById&lpar;R&period;id&period;view&lowbar;pager&lowbar;yogashan&rpar;&rpar;&period;setAdapter&lpar;new ImagePagerAdapter&lpar;&rpar;&rpar;&semi;&NewLine; return view&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; private class ImagePagerAdapter extends PagerAdapter &lbrace;&NewLine; private int&lbrack;&rsqb; mImages&semi;&NewLine; private String&lbrack;&rsqb; poseDesc&semi;&NewLine;&NewLine; private ImagePagerAdapter&lpar;&rpar; &lbrace;&NewLine; this&period;mImages &equals; new int&lbrack;&rsqb;&lbrace;R&period;drawable&period;bitilasan&comma; R&period;drawable&period;ardhahalsan&comma; R&period;drawable&period;majariasan&comma; R&period;drawable&period;sarvangasan&comma; R&period;drawable&period;trikonasan&comma; R&period;drawable&period;yogic&lowbar;jugging&rcub;&semi;&NewLine; this&period;poseDesc &equals; new String&lbrack;&rsqb;&lbrace;"1&period;Begin in a tabletop position&comma; on your hands and your knees&comma; with your knees stacked directly below your hips and your wrists&comma; elbows and shoulder in line and perpendicular to the floor&bsol;n&bsol;n2&period;Center your head in a neutral position as you gaze as the floor&bsol;n&bsol;n3&period;As you inhale&comma; lift your sitting bones and sternum toward the ceiling&comma; as your belly sinks toward the floor&bsol;n&bsol;n4&period;Lift your head and gaze up"&comma; "1&period;Lie down on your back and breath in&period; Pull your legs at 90 degrees and slowly try to take your legs towards your head&period;&bsol;n&bsol;n2&period;Ensure that you focus on your breathing and don&bsol;u2019t lose control&period;&bsol;n&bsol;n3&period;Only extend your body as easily as possible&period; Don&bsol;u2019t over exert and slowly let your body become more flexible&period;"&comma; "1&period; Stand on all fours&comma; such that your back forms a table top and your feet and hands from its legs&period;&bsol;n&bsol;n2&period; Your arms should be perpendicular to the floor&comma; and your hands should be placed flat on the floor&comma; right under your shoulders&period; Your knees should be placed hip-width apart&period;&bsol;n&bsol;n3&period; Look straight ahead&period;&bsol;n&bsol;n4&period; Inhale and raise your chin as you tilt your head backwards&period; Push your navel down and raise your tailbone&period; Compress your buttocks&period; You might feel a tingling sensation&period;&bsol;n&bsol;n5&period; Hold the pose for a few breaths&period; Breathe long and deep&period;"&comma; "1&period;Lie on your back with hands by your side&period;&bsol;n&bsol;n2&period;With one movement&comma; lift your legs&comma; buttocks and back so that you come up high on your shoulders&period; Support your back with the hands&period;&bsol;n&bsol;n3&period;Move your elbows closer towards each other&comma; and move your hands along your back&comma; creeping up towards the shoulder blades&period; Keep straightening the legs and spine by pressing the elbows down to the floor and hands into the back&period; Your weight should be supported on your shoulders and upper arms and not on your head and neck&period;&bsol;n&bsol;n4&period;Keep the legs firm&period; Lift your heels higher as though you are putting a footprint on the ceiling&period; Bring the big toes straight over the nose&period; Now point the toes up&period; Pay attention to your neck&period; Do not press the neck into the floor&period; Instead keep the neck strong with a feeling of tightening the neck muscles slightly&period; Press your sternum toward the chin&period; If you feel any strain in the neck&comma; come out of the posture&period;&bsol;n&bsol;n5&period;Keep breathing deeply and stay in the posture for 30-60 seconds&period;&bsol;n&bsol;n6&period;To come out of the posture&comma; lower the knees to forehead&period; Bring your hands to the floor&comma; palms facing down&period; Without lifting the head slowly bring your spine down&comma; vertebra by vertebra&comma; completely to the floor&period; Lower the legs to the floor&period; Relax for a minimum of 60 seconds&period;"&comma; "1&period;Stand with your feet wide apart as much as possible and at a comfortable distance without losing your balance and poise&period;&bsol;n&bsol;n2&period;Stretch your arms at your sides and breath in&period; As you breathe in&comma; bring your right hand next to the outside of the left foot and keep the other arm outstretched&period;&bsol;n&bsol;n3&period;Hold in this position for as long as possible&period; Do not bend your knees or arms&period; Try to stretch as much as possible&period; "&comma; "1&period;You have to stand on both your feet&period;&bsol;n&bsol;n2&period;Stretch the arm at shoulder level and the second arm elbow bent at 90 degrees next to your body&period;&bsol;n&bsol;n3&period;Now&comma; breathe in&comma; extend the folded arms and retract the arm that you had extended&period; At the same time&comma; move your leg opposite to the extended arm and ensure you lift it up as much as possible&period;&bsol;n&bsol;n4&period;This full extension of the arms and legs leaves you breathless and burns a lot of calories&comma; expands your lung capacity and helps streamline the blood purification in the body&period;"&rcub;&semi;&NewLine; &rcub;&NewLine;&NewLine; public int getCount&lpar;&rpar; &lbrace;&NewLine;&NewLine; return this&period;mImages&period;length&semi;&NewLine; &rcub;&NewLine;&NewLine; public boolean isViewFromObject&lpar;View view&comma; Object object&rpar; &lbrace;&NewLine; return view &equals;&equals; &lpar;&lpar;View&rpar; object&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public Object instantiateItem&lpar;ViewGroup container&comma; int position&rpar; &lbrace;&NewLine; FragmentActivity activity &equals; getActivity&lpar;&rpar;&semi;&NewLine;&NewLine; View view &equals; &lpar;&lpar;LayoutInflater&rpar; activity&period;getSystemService&lpar;Context&period;LAYOUT&lowbar;INFLATER&lowbar;SERVICE&rpar;&rpar;&period;inflate&lpar;R&period;layout&period;surya&lowbar;namaskar&lowbar;content&comma; container&comma; false&rpar;&semi;&NewLine; &lpar;&lpar;TextView&rpar; view&period;findViewById&lpar;R&period;id&period;textView&rpar;&rpar;&period;setText&lpar;this&period;poseDesc&lbrack;position&rsqb;&rpar;&semi;&NewLine; ImageView imageView &equals; &lpar;ImageView&rpar; view&period;findViewById&lpar;R&period;id&period;imageView&rpar;&semi;&NewLine; imageView&period;setScaleType&lpar;ImageView&period;ScaleType&period;FIT&lowbar;CENTER&rpar;&semi;&NewLine; imageView&period;setImageResource&lpar;this&period;mImages&lbrack;position&rsqb;&rpar;&semi;&NewLine; &lpar;&lpar;ViewPager&rpar; container&rpar;&period;addView&lpar;view&comma; 0&rpar;&semi;&NewLine; return view&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; public void destroyItem&lpar;ViewGroup container&comma; int position&comma; Object object&rpar; &lbrace;&NewLine; &lpar;&lpar;ViewPager&rpar; container&rpar;&period;removeView&lpar;&lpar;View&rpar; object&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>SuryaNamaskar&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;androidviewpagerexample&period;fragment&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v4&period;app&period;Fragment&semi;&NewLine;import android&period;support&period;v4&period;app&period;FragmentActivity&semi;&NewLine;import android&period;support&period;v4&period;view&period;PagerAdapter&semi;&NewLine;import android&period;support&period;v4&period;view&period;ViewPager&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;view&period;ViewGroup&semi;&NewLine;import android&period;widget&period;ImageView&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;&NewLine;import com&period;c1ctech&period;androidviewpagerexample&period;R&semi;&NewLine;&NewLine;&NewLine;public class SuryaNamaskar extends Fragment &lbrace;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public View onCreateView&lpar;LayoutInflater inflater&comma; ViewGroup container&comma;&NewLine; Bundle savedInstanceState&rpar; &lbrace;&NewLine; View view&equals; inflater&period;inflate&lpar;R&period;layout&period;fragment&lowbar;surya&lowbar;namaskar&comma; container&comma; false&rpar;&semi;&NewLine; &lpar;&lpar;ViewPager&rpar; view&period;findViewById&lpar;R&period;id&period;view&lowbar;pager&lowbar;suryanamaskar&rpar;&rpar;&period;setAdapter&lpar;new ImagePagerAdapter&lpar;&rpar;&rpar;&semi;&NewLine; return view&semi;&NewLine; &rcub;&NewLine;&NewLine; private class ImagePagerAdapter extends PagerAdapter &lbrace;&NewLine; private int&lbrack;&rsqb; mImages&semi;&NewLine; private String&lbrack;&rsqb; poseDesc&semi;&NewLine;&NewLine; private ImagePagerAdapter&lpar;&rpar; &lbrace;&NewLine; this&period;mImages &equals; new int&lbrack;&rsqb;&lbrace;R&period;drawable&period;posepre&comma; R&period;drawable&period;pose1&comma; R&period;drawable&period;pose2&comma; R&period;drawable&period;pose3&comma; R&period;drawable&period;pose4&comma; R&period;drawable&period;pose5&comma; R&period;drawable&period;pose6&comma; R&period;drawable&period;pose7&comma; R&period;drawable&period;pose8&comma; R&period;drawable&period;pose9&comma; R&period;drawable&period;pose10&rcub;&semi;&NewLine; this&period;poseDesc &equals; new String&lbrack;&rsqb;&lbrace;"Inhale&colon; As you breathe in&comma; lift both arms up from the sides&period;&bsol;nExhale&colon; As you exhale&comma; bring your palms together in front of the chest in prayer position&period;"&comma; "Inhale &colon; Breathing in&comma; lift the arms up&period;"&comma; "Exhale &colon; Exhale completely&comma; bringing your hands down to the floor&period;"&comma; "Inhale &colon; Bring the right foot forward in between the two hands&comma; left knee down to the floor and look up&period;"&comma; "Inhale&colon; As you breathe in&comma; take the right leg back and bring the whole body in a straight line"&comma; "Exhale&colon; Gently bring your two hands&comma; two feet&comma; two knees&comma; chest and chin to touch the floor&period;"&comma; "Inhale&colon; Keep your elbows bent and look up&period;"&comma; "Exhale&colon; The inverted V&period;"&comma; "Inhale&colon; Bring the left foot forward in between the two hands&comma; right knee down to the floor and look up&period;"&comma; "Exhale&colon; Bring your right foot forward and try to keep your palms on the floor&period;"&comma; "Inhale&colon; Breathing in&comma; lift the arms up&period;"&rcub;&semi;&NewLine; &rcub;&NewLine;&NewLine; public int getCount&lpar;&rpar; &lbrace;&NewLine; return this&period;mImages&period;length&semi;&NewLine; &rcub;&NewLine;&NewLine; public boolean isViewFromObject&lpar;View view&comma; Object object&rpar; &lbrace;&NewLine; return view &equals;&equals; &lpar;&lpar;View&rpar; object&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public Object instantiateItem&lpar;ViewGroup container&comma; int position&rpar; &lbrace;&NewLine; FragmentActivity activity &equals; getActivity&lpar;&rpar;&semi;&NewLine; View view &equals; &lpar;&lpar;LayoutInflater&rpar; activity&period;getSystemService&lpar;"layout&lowbar;inflater"&rpar;&rpar;&period;inflate&lpar;R&period;layout&period;surya&lowbar;namaskar&lowbar;content&comma; container&comma; false&rpar;&semi;&NewLine; &lpar;&lpar;TextView&rpar; view&period;findViewById&lpar;R&period;id&period;textView&rpar;&rpar;&period;setText&lpar;this&period;poseDesc&lbrack;position&rsqb;&rpar;&semi;&NewLine; ImageView imageView &equals; &lpar;ImageView&rpar; view&period;findViewById&lpar;R&period;id&period;imageView&rpar;&semi;&NewLine; imageView&period;setScaleType&lpar;ImageView&period;ScaleType&period;FIT&lowbar;CENTER&rpar;&semi;&NewLine; imageView&period;setImageResource&lpar;this&period;mImages&lbrack;position&rsqb;&rpar;&semi;&NewLine; &lpar;&lpar;ViewPager&rpar; container&rpar;&period;addView&lpar;view&comma; 0&rpar;&semi;&NewLine; return view&semi;&NewLine; &rcub;&NewLine;&NewLine; public void destroyItem&lpar;ViewGroup container&comma; int position&comma; Object object&rpar; &lbrace;&NewLine; &lpar;&lpar;ViewPager&rpar; container&rpar;&period;removeView&lpar;&lpar;View&rpar; object&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Using FragmentPagerAdapter<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>This adapter is used when you want to swipe between the fragments and there is only a limited number of pages&lpar;Fragment&rpar; to show&period;<&sol;p>&NewLine;<p>Two main methods that we must override&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>getItem&lpar;int position&rpar;<&sol;strong> <&sol;span>&colon; returns a fragment of specific position&period;<br &sol;>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">getCount&lpar;&rpar;<&sol;span> <&sol;strong>&colon; counts the no&period; of fragments available&period;<&sol;p>&NewLine;<p><strong>7<&sol;strong>&period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>SharirikActivity&period;java<&sol;strong><&sol;span> as I have refracted <strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity&period;java<&sol;span><&sol;strong> as <strong><span style&equals;"color&colon; &num;008000&semi;">SharirikActivity&period;java<&sol;span><&sol;strong> and write the below code&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>SharirikActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;androidviewpagerexample&semi;&NewLine;&NewLine;import android&period;support&period;design&period;widget&period;TabLayout&semi;&NewLine;import android&period;support&period;v4&period;app&period;Fragment&semi;&NewLine;import android&period;support&period;v4&period;app&period;FragmentManager&semi;&NewLine;import android&period;support&period;v4&period;app&period;FragmentPagerAdapter&semi;&NewLine;import android&period;support&period;v4&period;view&period;ViewPager&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v7&period;widget&period;Toolbar&semi;&NewLine;&NewLine;import com&period;c1ctech&period;androidviewpagerexample&period;fragment&period;SuryaNamaskar&semi;&NewLine;import com&period;c1ctech&period;androidviewpagerexample&period;fragment&period;Yogasan&semi;&NewLine;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;public class SharirikActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; private Toolbar toolbar&semi;&NewLine; private TabLayout tabLayout&semi;&NewLine; private ViewPager viewPager&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;sharirik&rpar;&semi;&NewLine;&NewLine; toolbar &equals; &lpar;Toolbar&rpar; findViewById&lpar;R&period;id&period;toolbar&rpar;&semi;&NewLine; setSupportActionBar&lpar;toolbar&rpar;&semi;&NewLine;&NewLine; getSupportActionBar&lpar;&rpar;&semi;&NewLine;&NewLine; viewPager &equals; &lpar;ViewPager&rpar; findViewById&lpar;R&period;id&period;viewpager&rpar;&semi;&NewLine; setupViewPager&lpar;viewPager&rpar;&semi;&NewLine;&NewLine; tabLayout &equals; &lpar;TabLayout&rpar; findViewById&lpar;R&period;id&period;tabs&rpar;&semi;&NewLine; tabLayout&period;setupWithViewPager&lpar;viewPager&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private void setupViewPager&lpar;ViewPager viewPager&rpar; &lbrace;&NewLine; ViewPagerAdapter adapter &equals; new ViewPagerAdapter&lpar;getSupportFragmentManager&lpar;&rpar;&rpar;&semi;&NewLine; adapter&period;addFragment&lpar;new Yogasan&lpar;&rpar;&comma; "YOGASHAN"&rpar;&semi;&NewLine; adapter&period;addFragment&lpar;new SuryaNamaskar&lpar;&rpar;&comma; "SURYANAMASKAR"&rpar;&semi;&NewLine; viewPager&period;setAdapter&lpar;adapter&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; class ViewPagerAdapter extends FragmentPagerAdapter &lbrace;&NewLine; private final List&lt&semi;Fragment&gt&semi; mFragmentList &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine; private final List&lt&semi;String&gt&semi; mFragmentTitleList &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine;&NewLine; public ViewPagerAdapter&lpar;FragmentManager manager&rpar; &lbrace;&NewLine; super&lpar;manager&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public Fragment getItem&lpar;int position&rpar; &lbrace;&NewLine; return mFragmentList&period;get&lpar;position&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public int getCount&lpar;&rpar; &lbrace;&NewLine; return mFragmentList&period;size&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void addFragment&lpar;Fragment fragment&comma; String title&rpar; &lbrace;&NewLine; mFragmentList&period;add&lpar;fragment&rpar;&semi;&NewLine; mFragmentTitleList&period;add&lpar;title&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public CharSequence getPageTitle&lpar;int position&rpar; &lbrace;&NewLine; return mFragmentTitleList&period;get&lpar;position&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>When you run the app it will look like this as shown below&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-741 " src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;08&sol;Screenshot&lowbar;1534165815-576x1024&period;png" alt&equals;"" width&equals;"359" height&equals;"639" &sol;> <img class&equals;"alignnone wp-image-742 " src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;08&sol;Screenshot&lowbar;1534165821-576x1024&period;png" alt&equals;"" width&equals;"360" height&equals;"642" &sol;><&sol;p>&NewLine;<p>That&&num;8217&semi;s all for this tutorial &period;I hope it is helpful for you to understand the working of <strong><span style&equals;"color&colon; &num;008000&semi;">viewpager<&sol;span><&sol;strong> with pagerAdapter&comma;FragmentPagerAdapter&period;&NewLine;

Exit mobile version