This is a Kotlin solution. Set the object of this class as the adapter viewPager-
class PageAdapter : PagerAdapter() { private val pageLayouts = listOf(R.layout.home_page_1, R.layout.home_page_2) override fun instantiateItem(container: ViewGroup, position: Int): Any = LayoutInflater.from(container.context).inflate(pageLayouts[position], container, false).apply { container.addView(this) } override fun destroyItem(container: ViewGroup, position: Int, 'object': Any) = container.removeView('object' as View) override fun getCount(): Int = 2 override fun isViewFromObject(view: View, 'object': Any): Boolean = (view == 'object') }
source share