Skip to content

Latest commit

 

History

History
50 lines (42 loc) · 1.79 KB

README.md

File metadata and controls

50 lines (42 loc) · 1.79 KB

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