introduction of view pager in android

4
ACADGILD ACADGILD Introduction: ViewPager is one of the widgets of Android and it supports library android.support.v4.view.ViewPager”. It is used to slide the screens from left to right or right to left with different pages. We can implement ViewPager with or without using Fragments. In my example it uses fragments. Real Time Example: We can see the functionality of ViewPager in many News applications. Classes & Methods: Classes: Create a fragment class ScreenSlidePageFragment it returns the layout by overriding onCreateView() method. Create inner class ScreenSlidePageAdapter which extends FragmentStatePagerAdapter in your MainActivity. It is a simple pager adapter that shows 5 ScreenSlidePageFragment objects in a sequence. Methods: getItem(): It returns the instance of a fragment as new pages. getCount(): It returns the number of pages. Example with code: 1.Create a new Application AndroidViewPagerExample. 2.Create two layout file one for main activity and another for a fragment. Create XML files for Views: fragment_screen_slide_page.xml: Here it shows the content of a fragment, it contains a TextView inside ScrollView to display the text on it. <TextView style="?android:textAppearanceMedium" android:layout_width="match_parent" https://acadgild.com/blog/wp-admin/post.php?post=11468&action=edit https://acadgild.com/blog/wp-admin/post.php?post=11468&action=edit

Upload: acadgild

Post on 18-Feb-2017

54 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Introduction of view pager in android

ACADGILDACADGILD

Introduction:

ViewPager is one of the widgets of Android and it supports library “android.support.v4.view.ViewPager”.

It is used to slide the screens from left to right or right to left with different pages.

We can implement ViewPager with or without using Fragments. In my example it uses fragments.

Real Time Example:

We can see the functionality of ViewPager in many News applications.

Classes & Methods:

Classes:

Create a fragment class ScreenSlidePageFragment it returns the layout by overriding onCreateView() method.

Create inner class ScreenSlidePageAdapter which extends FragmentStatePagerAdapter in your MainActivity. It is a simple pager adapter that shows 5 ScreenSlidePageFragment objects in a sequence.

Methods:

getItem(): It returns the instance of a fragment as new pages.

getCount(): It returns the number of pages.

Example with code:

1.Create a new Application AndroidViewPagerExample.2.Create two layout file one for main activity and another for a fragment.

Create XML files for Views:

fragment_screen_slide_page.xml:

Here it shows the content of a fragment, it contains a TextView inside ScrollView to display the text on it.<TextView style="?android:textAppearanceMedium" android:layout_width="match_parent"

https://acadgild.com/blog/wp-admin/post.php?post=11468&action=edithttps://acadgild.com/blog/wp-admin/post.php?post=11468&action=edit

Page 2: Introduction of view pager in android

ACADGILDACADGILD

android:layout_height="wrap_content" android:lineSpacingMultiplier="1.2" android:padding="16dp" android:text="@string/text" />

Add the text in strings.xml file.

activity_main.xml :

In this file we create a ViewPager and a TextView to show the page number on it.<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" android:background="#00afff" />

<TextView android:id="@+id/number" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center|bottom" android:textStyle="bold" />

Create the Fragment:

ScreenSlidePageFragment.java :

Here we create the fragment class ScreenSlidePageFragment as I discussed above,it extends with Fragment, override the onCreateView() method. Inside this

Add the code to return view.ViewGroup rootView = (ViewGroup) inflater.inflate( R.layout.fragment_screen_slide_page, container, false);

return rootView;

Create the MainActivity:

MainActivity.java :

Here create the class MainActivity and extends with FragmentActivity

Initialize :private ViewPager mPager;private PagerAdapter mPagerAdapter;

https://acadgild.com/blog/wp-admin/post.php?post=11468&action=edithttps://acadgild.com/blog/wp-admin/post.php?post=11468&action=edit

Page 3: Introduction of view pager in android

ACADGILDACADGILD

Add the code in onCreate(){..}Here take the reference of ViewPager and set the PagerAdapter.mPager = (ViewPager) findViewById(R.id.pager);mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());mPager.setAdapter(mPagerAdapter);

Create the inner class ScreenSlidePagerAdapter in this activity.

Add the code in for getItem() and getCount() :@Override public Fragment getItem(int position) { return new ScreenSlidePageFragment(); }

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

To handle the device back button implement this:

Add the code in onBackPressed() method:if (mPager.getCurrentItem() == 0) {super.onBackPressed();} else {//select the previous step.mPager.setCurrentItem(mPager.getCurrentItem() - 1)}

To show the page number on the screens :

We have to create an inner class to show the screen position for the user.

Add the code in same activity.private class PageListener extends SimpleOnPageChangeListener{ public void onPageSelected(int position) { currentPage = position; textNumber.setText("Page :" + currentPage); }}

So, these are the minimum requirements to create ViewPager in your application.

Output:

https://acadgild.com/blog/wp-admin/post.php?post=11468&action=edithttps://acadgild.com/blog/wp-admin/post.php?post=11468&action=edit

Page 4: Introduction of view pager in android

ACADGILDACADGILD

Conclusion:

This is the basic idea of creating ViewPager in your application. Its use is to swipe the pages or screens. We can customize it more by adding images, etc according to the need of an application. As I give the example of News application, we can see the same functionality of ViewPager. Here I create the ViewPager by using fragments, we can also create the instance of a fragment for different pages. This feature is very popular in Android development.

https://acadgild.com/blog/wp-admin/post.php?post=11468&action=edithttps://acadgild.com/blog/wp-admin/post.php?post=11468&action=edit