Skip to content

VijayMakwana/EasyPagerAdapter

Repository files navigation

EasyPagerAdapter License

Easiest android ViewPager adapter implementation, Don't write ViewPager adapter ever again

  • Based on Android Data Binding

  • Written in Kotlin

  • No need to write the ViewPager adapter

  • No need to modify your model classes

  • Minimum API Level support 21

  • Supports multiple item view types

  • Optional Callbacks/Listeners

  • Efficient and easy API

  • Tiny in size

  • Support Kotlin and Java

  • Support AndroidX

  • Support ViewPager2

Setup

Migrate the project to AndroidX, do the following steps for migration Refactor -> Migrate to AndroidX

Gradle

Add this in your project level build.gradle file (not your module build.gradle file):

allprojects {
 repositories {
        maven { url "https://jitpack.io" } // add this line
    }
}

Then, add the library to your module build.gradle

// apply plugin: 'kotlin-kapt' // this line only for Kotlin projects

android {
    ...
    buildFeatures {
      dataBinding true
  	} 
}

dependencies {
 implementation 'com.github.VijayMakwana:EasyPagerAdapter:1.1.0'
}

Usage

Create your item layouts with <layout> as root:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable name="item"
                  type="com.easypageradapter.easypageradaptersample.data.PersonDetail"/>
    </data>
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@{item.name}"/>
        
</layout>
// Kotlin
mBinding.viewPager.setEasyPagerAdapter(mPageList, BR.item)
                .map<ImageModel>(R.layout.item_view_pager_image)
                .map<PersonDetail>(R.layout.item_view_pager_person_detail_item)
// Java
new EasyPagerAdapter(mPageList, BR.item)
                .map(ImageModel.class, R.layout.item_view_pager_image)
                .map(PersonDetail.class, R.layout.item_view_pager_person_detail_item)
                .into(mBinding.viewPager);

If you want to attach callbacks or listeners then you can do like this

// Kotlin sample
     mBinding.viewPager.setEasyPagerAdapter(mPageList)
                .map<ImageModel>(R.layout.item_view_pager_image, BR.item)
                .map<PersonDetail, ItemViewPagerPersonDetailBinding>
         (R.layout.item_view_pager_person_detail, BR.person)
                { itemBind ->
                    itemBind.btnSubmit.setOnClickListener {
                     Toast.makeText(this@KotlinDemoActivity,
                     "Submit Button Clicked in the Person ${itemBind.person?.name} Page",
                           Toast.LENGTH_LONG).show()
                    }
                }
// Java sample
new EasyPagerAdapter(mPageList)
          .map(ImageModel.class, R.layout.item_view_pager_image, BR.item)
          .map(PersonDetail.class,
              R.layout.item_view_pager_person_detail,
              BR.person, new PagerCallBack<ItemViewPagerPersonDetailBinding>() {
                  @Override
                  public void onBind(final ItemViewPagerPersonDetailBinding itemBind) {
                     itemBind.btnSubmit.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View view) {
                        Toast.makeText(JavaDemoActivity.this,
                         "Submit Button Clicked in the Person " + 
                itemBind.getPerson().getName() +
                           " Page", Toast.LENGTH_LONG).show();
                                    }
                                });
                            }
                        })
                .into(mBinding.viewPager);

set fragment pager adapter

 mBinding.viewPager.setEasyFragmentPagerAdapter
 (supportFragmentManager, listOf(FragmentA(), FragmentB()))

set fragment state pager adapter

 mBinding.viewPager.setEasyFragmentStatePagerAdapter
 (supportFragmentManager, listOf(FragmentA(), FragmentB()))

set viewpager2 fragment state adapter

// ViewPager2
mBinding.viewPager2.setEasyFragmentStateAdapter(fragmmentActivity, listOf(FragmentA(), FragmentB(),FragmentC(), FragmentD()))

Check out the wiki for detailed documentation and usage examples.

Wiki

Special thanks to LastAdapter by @Miguel Ángel Moreno for inspiring this library