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.

DEMO

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=”http://schemas.android.com/apk/res/android”

xmlns:app=”http://schemas.android.com/apk/res-auto”…/>

<androidx.viewpager2.widget.ViewPager2

android:id=”@+id/view_pager”

android:layout_width=”match_parent”

android:layout_height=”match_parent”/>

<RelativeLayout/>

open class ViewsSliderActivity expands AppCompatActivity {

private ViewsSliderAdapter mAdapter;

private TextView[] spots;

private int[] formats;

private ActivityViewsSliderBinding authoritative;

@Override

secured void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

restricting = ActivityViewsSliderBinding.inflate(getLayoutInflater());

setContentView(binding.getRoot());

init();

}

private void init() {

/formats of every invite slider

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

formats = new int[]{

R.layout.slide_one,

R.layout.slide_two,

R.layout.slide_three,

R.layout.slide_four};

mAdapter = new ViewsSliderAdapter();

binding.viewPager.setAdapter(mAdapter);

binding.viewPager.registerOnPageChangeCallback(pageChangeCallback);

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

binding.viewPager.setCurrentItem(current);

} else {

launchHomeScreen();

}

});

binding.iconMore.setOnClickListener(view – > {

showMenu(view);

});

/including base spots

addBottomDots(0);

}

/*

* 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);

binding.layoutDots.removeAllViews();

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

dots[i] = new TextView(this);

dots[i].setText(Html.fromHtml(“&#8226;”));

dots[i].setTextSize(35);

dots[i].setTextColor(colorsInactive[currentPage]);

binding.layoutDots.addView(dots[i]);

}

in the event that (dots.length > 0)

dots[currentPage].setTextColor(colorsActive[currentPage]);

}

private int getItem(int I) {

return binding.viewPager.getCurrentItem() + I;

}

private void launchHomeScreen() {

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

finish();

}

private void showMenu(View see) {

PopupMenu popup = new PopupMenu(this, see);

MenuInflater inflater = popup.getMenuInflater();

inflater.inflate(R.menu.pager_transformers, popup.getMenu());

popup.setOnMenuItemClickListener(item – > {

in the event that (item.getItemId() == R.id.action_orientation) {

binding.viewPager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);

} else {

binding.viewPager.setPageTransformer(Utils.getTransformer(item.getItemId()));

binding.viewPager.setCurrentItem(0);

binding.viewPager.getAdapter().notifyDataSetChanged();

}

return bogus;

});

popup.show();

}

/*

* ViewPager page change audience

*/

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

@Override

open void onPageSelected(int position) {

super.onPageSelected(position);

addBottomDots(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

binding.btnNext.setText(getString(R.string.start));

binding.btnSkip.setVisibility(View.GONE);

} else {

/still pages are left

binding.btnNext.setText(getString(R.string.next));

binding.btnSkip.setVisibility(View.VISIBLE);

}

}

};

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

open ViewsSliderAdapter() {

}

@NonNull

@Override

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

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

.inflate(viewType, parent, bogus);

return new SliderViewHolder(view);

}

@Override

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

}

@Override

open int getItemViewType(int position) {

return layouts[position];

}

@Override

open int getItemCount() {

return layouts.length;

}

open class SliderViewHolder broadens RecyclerView.ViewHolder {

open TextView title, year, type;

open SliderViewHolder(View see) {

super(view);

}

}

}

}

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 ‘com.google.android.material:material:1.2.0-alpha01’

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 androidx.fragment.app.Fragment;

import info.androidhive.viewpager2.R;

open class MoviesFragment broadens Fragment {

@Override

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.

<androidx.viewpager2.widget.ViewPager2

android:orientation=”vertical”

android:layout_width=”match_parent”

android:layout_height=”wrap_content”/>

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

viewPager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);

Leave a Reply

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