Skip to content

ViewPager2 comes with major improvements from ViewPager which enables swipe views with RecyclerView, vertical swipe, orientation, right-to-left support, Modifiable fragment collection, PageTransformations, swipe between fragments with TabLayout, DiffUtils etc. Let's understand with an example implementation.

Notifications You must be signed in to change notification settings

RajashekarRaju/RecyclerViewToViewPager

Repository files navigation

RecyclerViewToViewPager

Android app repository which demonstrates on imlementing RecyclerView to ViewPager2 along with jetpack components ViewModel, LiveData, NavigationComponent, Architecture.

ReyclerView to ViewPager

Dependencies

implementation "androidx.viewpager2:viewpager2:1.0.0"
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

Add ViewPager2 to xml layout

<androidx.viewpager2.widget.ViewPager2
    android:id="@+id/my_view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" />

ViewPager2 in Kotlin class

// Get reference to ViewPager2
val viewPager2: ViewPager2 = view.findViewById(R.id.detail_view_pager)
// Create a new ViewPagerAdapter class
val viewPagerAdapter = ViewPagerAdapter()
// Add items to our recyclerView adpater
viewPagerAdapter.submitList(yourItemsList)
// Attach ViewPager2 with Adapter
viewPager2.adapter = viewPagerAdapter
// Get user clicked item position and set it to ViewPager2
viewPager2.setCurrentItem(selectedItem)

Implementation has been clearly explained in blog (https://developersbreach.com)

This repository has following branches which differs in features stated below.

Branch - Master

Without DataBinding

Branch - DataBinding

Applied to ViewModels, Layouts, Fragment, Two-Way Binding

Branch - SimpleExample

No DataBiding, Removed Material ViewPager2, CustomViews Removed

About

ViewPager2 comes with major improvements from ViewPager which enables swipe views with RecyclerView, vertical swipe, orientation, right-to-left support, Modifiable fragment collection, PageTransformations, swipe between fragments with TabLayout, DiffUtils etc. Let's understand with an example implementation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages