
What is ViewPager ?
ViewPager is Android allows the user to flip left and right through pages of data. user can swipe left to right in ViewPager. it is used to Fragments.
ViewPager has the ability to dynamically add and remove pages (or tabs) at anytime. ViewPager are user could then swipe left or right to see other categorized lists. Using the ViewPager requires some knowledge of both Fragments and PageAdapters. ViewPager provides the functionality to flip pages in app.
Pager Adapter In ViewPager
Android ViewPager is a layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.
PagerAdapter providing the adapter to populate pages inside of a ViewPager.
It does not Required Fragment you can use only xml layout file to attach in view pager.
It is limited (fixed) number of items (Fragments). PagerAdapter never removes a fragment instance from FragmentManager.
PagerAdapter used to layout xml file only it does not required fragments create.its create simple xml layout file and then attach the layout file from pagerAdapter.
you can Implement pagerAdapter to you must override methods in PagerAdapter.
ViewPager are two type of Adapter : PagerAdapter and FragmentStatePagerAdapter
PagerAdapter Methods in Android
instantiateItem(ViewGroup, int) : In this method is used to create the page for the given position passed to it as an argument. You can inflate layout file and then set resource in layout file. it is return of view objects.
destroyItem(ViewGroup, int, Object) : In this method is used to Removes the page from the container for the given position.you can removed object using removeView but could’ve also used removeViewAt() method by passing it the position.
getCount() : In this method is used to return the number of views available.
isViewFromObject(View, Object) : In this method is used to returned by instantiateItem() is a key/identifier. This method check the View passed or not. It is required by a PagerAdapter.
FragmentStatePagerAdapter in ViewPager Android
In this adapter is required Fragments. you can use this adapter to required creates fragments file.
A FragmentStatePagerAdapter is more memory savvy. It is removed fragment instance from FragmentManager.
The state of the removed Fragments is stored inside the FragmentStatePagerAdapter. It is better the content of the fragments are static than something that constantly changes or gets updated.
ViewPager Example in Android
Implements below This Library in build.gradle in App Module
1 |
implementation 'com.android.support:design:26.+' |
Create activity_main.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.bhaumik.MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="fill" app:tabMode="fixed" app:tabBackground="@color/colorPrimaryDark" app:tabSelectedTextColor="#fff"/> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> |
Create fragment_first.xml File Follow this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.bhaumik.FirstFragment" android:background="@android:color/holo_blue_dark"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="@dimen/_20dp" android:textColor="#fff" android:textStyle="bold" android:gravity="center" android:text="This is a First Fragment" /> </FrameLayout> |
Create fragment_second.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.bhaumik.SecondFragment" android:background="@android:color/holo_green_dark"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="@dimen/_20dp" android:textColor="#fff" android:gravity="center" android:textStyle="bold" android:text="This is a Second Fragment" /> </FrameLayout> |
Create FirstFragment.java File Follow this Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.example.bhaumik; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.bhaumik.R; public class FirstFragment extends Fragment { public FirstFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_first, container, false); } } |
Create SecondFragment.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.example.bhaumik; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.bhaumik.R; /** * A simple {@link Fragment} subclass. */ public class SecondFragment extends Fragment { public SecondFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_second, container, false); } } |
Create MainActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.example.bhaumik; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.example.bhaumik.ViewPagerAdapter; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); tabLayout.setupWithViewPager(viewPager); viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); } } |
Create ViewPagerAdapter.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.example.bhaumik; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import com.example.bhaumik.FirstFragment; import com.example.bhaumik.SecondFragment; public class ViewPagerAdapter extends FragmentStatePagerAdapter{ public ViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position){ case 0 : return getFirstFragment(); case 1 : return getSecondFragment(); } return null; } @Override public int getCount() { return 2; } private FirstFragment getFirstFragment() { FirstFragment firstFragment = new FirstFragment(); Bundle bundle = new Bundle(); firstFragment.setArguments(bundle); return firstFragment; } public SecondFragment getSecondFragment() { SecondFragment secondFragment = new SecondFragment(); Bundle bundle = new Bundle(); secondFragment.setArguments(bundle); return secondFragment; } @Override public CharSequence getPageTitle(int position) { switch (position){ case 0 : return "First Fragment"; case 1 : return "Second Fragment"; } return ""; } } |
In this Example of Create Simple ViewPager and Tabs and create two fragments and in this fragments implement ViewPager adapter class.In this adapter to count fragments and display fragments.
you can select first tab to open First Fragment. and you can click second tab to open Second Fragment screen.