Android working with ViewPager2-Gossip Buzzer

As we as a whole know, the Android group is continually discharging new updates and enhancements to the Android Framework. One of the segments that got significant updates is ViewPager2. ViewPager2 is the substitution of ViewPager and got scarcely any exhibition enhancements and extra functionalities. So as to utilize ViewPager2, you ought to consider utilizing androidx antiques in your task.

In my previous instructional exercise, I have disclosed Building Intro Sliders to your App utilizing ViewPager. In this article, we are going to attempt the equivalent however utilizing ViewPager2. We’ll likewise experience the extra intriguing functionalities gave by ViewPager2.


Whats’ New in ViewPager2

ViewPager2 is extemporized adaptation of ViewPager that gives extra functionalities and addresses regular issues looked in ViewPager.

ViewPager2 is based on head of RecyclerView. So you will have extraordinary points of interest that a RecyclerView has. For instance, you can exploit DiffUtils to effectively figure the distinction between informational collections and update the ViewPager with movements.

ViewPager2 bolsters vertical direction. Prior vertical swiping is finished by tweaking the ViewPager.

ViewPager2 has right-to-left(RTL) backing and this will be empowered consequently dependent on application region.

When working with an assortment of Fragments, calling notifyDatasetChanged() will refresh the UI appropriately when a basic Fragment changes its UI.

ViewPager2 bolsters page changes i.e you can give livelinesss when exchanging between pages. You can likewise compose your own custom page change.

PagerAdapter is supplanted by RecyclerView.Adapter as its dependent on RecyclerView.

FragmentStatePagerAdapter is supplanted by FragmentStateAdapter.

To begin with ViewPager2, add the reliance to application/build.gradle.

usage “androidx.viewpager2:viewpager2:1.0.0″

1. ViewPager with Static Views

For the most part, ViewPager is utilized to accomplish a selected view by consolidating the TabLayout and assortment of Fragments. In any case, on the off chance that you need to have static perspectives without the Fragment class, you can do it utilizing RecyclerView connector class.

The following is a case of executing Intro Slides utilizing ViewPager2. The structures are taken from my more seasoned model How to Build Intro Slider for your App yet the usage part fluctuates because of changes in ViewPager2 and in connector class.

Add the ViewPager2 to your format document. You can see namespace is utilizes androidx bundle androidx.viewpager2.widget.ViewPager2.

<RelativeLayout xmlns:android=””







open class ViewsSliderActivity expands AppCompatActivity {

private ViewsSliderAdapter mAdapter;

private TextView[] spots;

private int[] formats;

private ActivityViewsSliderBinding authoritative;


secured void onCreate(Bundle savedInstanceState) {


restricting = ActivityViewsSliderBinding.inflate(getLayoutInflater());




private void init() {

/formats of every invite slider

/include hardly any more designs in the event that you need

formats = new int[]{





mAdapter = new ViewsSliderAdapter();



binding.btnSkip.setOnClickListener(v – > launchHomeScreen());

binding.btnNext.setOnClickListener(v – > {

/checking for last page

/if last page home screen will be propelled

int current = getItem(+1);

in the event that (current < layouts.length) {

/move to next screen


} else {




binding.iconMore.setOnClickListener(view – > {



/including base spots




* Adds base spots pointer


private void addBottomDots(int currentPage) {

spots = new TextView[layouts.length];

int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);

int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);


for (int I = 0; I < dots.length; i++) {

dots[i] = new TextView(this);






in the event that (dots.length > 0)



private int getItem(int I) {

return binding.viewPager.getCurrentItem() + I;


private void launchHomeScreen() {

Toast.makeText(this, R.string.slides_ended, Toast.LENGTH_LONG).show();



private void showMenu(View see) {

PopupMenu popup = new PopupMenu(this, see);

MenuInflater inflater = popup.getMenuInflater();

inflater.inflate(, popup.getMenu());

popup.setOnMenuItemClickListener(item – > {

in the event that (item.getItemId() == {


} else {





return bogus;




* ViewPager page change audience


ViewPager2.OnPageChangeCallback pageChangeCallback = new ViewPager2.OnPageChangeCallback() {


open void onPageSelected(int position) {



/changing the following catch text ‘NEXT’/’GOT IT’

in the event that (position == layouts.length – 1) {

/last page. make button text to GOT IT



} else {

/still pages are left






open class ViewsSliderAdapter broadens RecyclerView.Adapter<RecyclerView.ViewHolder> {

open ViewsSliderAdapter() {




open RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View see = LayoutInflater.from(parent.getContext())

.inflate(viewType, parent, bogus);

return new SliderViewHolder(view);



open void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {



open int getItemViewType(int position) {

return layouts[position];



open int getItemCount() {

return layouts.length;


open class SliderViewHolder broadens RecyclerView.ViewHolder {

open TextView title, year, type;

open SliderViewHolder(View see) {






This makes on a level plane swipeable perspectives that are made utilizing static XML formats. The full code of this model can be found here.

2. ViewPager with Tabs and Fragments

On the off chance that you need to make swipeable perspectives with Tabs, you can consolidate ViewPager2, TabLayout and Fragments.

To utilize TabLayout, add Material Components to your bundle. This makes the material TabLayout accessible in your venture.

execution ‘’

Add TabLayout and ViewPage2 to your format.

Make required test Fragment classes MoviesFragment, EventsFragment and TicketsFragment.

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;


import info.androidhive.viewpager2.R;

open class MoviesFragment broadens Fragment {


general visibility onCreateView(

LayoutInflater inflater, ViewGroup holder,

Group savedInstanceState

) {

/Inflate the design for this part

return inflater.inflate(R.layout.fragment_movies, holder, bogus);



At long last, make a connector class that gives Fragments to ViewPager. Here we can see ViewPagerFragmentAdapter broadens FragmentStateAdapter.

3. ViewPager2 Orientation

In a couple of situations, you should give vertical swiping rather than customary flat swiping. To empower vertical swiping, add android:orientation to ViewPager2 component.





Vertical direction can likewise be empowered automatically by calling setOrientation() technique.


Leave a Reply

Your email address will not be published. Required fields are marked *