ViewPager2 replaces ViewPager
, addressing most of its predecessor’s pain-points, including right-to-left layout support, vertical orientation, modifiable Fragment collections, etc. Now its stable version is also released.
To use ViewPager2 in your projects, you need to add the following dependencies in your build.gradle file :
1 2 3 |
dependencies { implementation "androidx.viewpager2:viewpager2:1.0.0" } |
To add ViewPager2 in your layout, add the following code in your layout’s XML file :
1 2 3 4 |
<androidx.viewpager2.widget.ViewPager2 android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"/> |
If you check the implementation of ViewPager2, it uses RecyclerView within. Thus if you’ve implemented RecyclerView, implementing ViewPager2 wouldn’t be much different.
You can use fragments inside ViewPager2 same as we do in ViewPager.
You just need to use FragmentStateAdapter
instead of FragmentStatePagerAdapter
.
In order to register a Listener on ViewPager2, we need to do the following:
1 2 3 4 5 6 |
view_pager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { super.onPageScrolled(position, positionOffset, positionOffsetPixels); } }); |
To set scroll orientation we need to do the following:
1 |
viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL); //To set horizontal swipe/scroll |
OR
1 |
viewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL); //To set vertical swipe/scroll |
Example of an adapter for the same:
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 73 74 |
package com.webkul.androidviewpager2; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import java.util.List; import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager2.widget.ViewPager2; public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewHolder> { private List<String> mData; private LayoutInflater mInflater; private ViewPager2 viewPager2; private int[] colorArray = new int[]{android.R.color.black, android.R.color.holo_blue_dark, android.R.color.holo_green_dark, android.R.color.holo_red_dark}; ViewPagerAdapter(Context context, List<String> data, ViewPager2 viewPager2) { this.mInflater = LayoutInflater.from(context); this.mData = data; this.viewPager2 = viewPager2; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflater.inflate(R.layout.item_viewpager, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData.get(position); holder.myTextView.setText(animal); holder.relativeLayout.setBackgroundResource(colorArray[position]); } @Override public int getItemCount() { return mData.size(); } public class ViewHolder extends RecyclerView.ViewHolder { TextView myTextView; RelativeLayout relativeLayout; Button button; ViewHolder(View itemView) { super(itemView); myTextView = itemView.findViewById(R.id.tvTitle); relativeLayout = itemView.findViewById(R.id.container); button = itemView.findViewById(R.id.btnToggle); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(viewPager2.getOrientation() == ViewPager2.ORIENTATION_VERTICAL) viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL); else{ viewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL); } } }); } } } |
References: