http://thoughtnerds.com/viewpager-using-kotlin-android-tutorial/
ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we’ll implement a ViewPager that swipes through three views. This tutorial to show how implement ViewPager Using Kotlin Android .- Create a Model enum for getting three view Model.kt
package com.cretlabs.viewpager
/**
* Created by Gokul on 2/11/2018.
*/
enum class Model private constructor(val titleResId: Int, val layoutResId: Int) {
RED(R.string.one, R.layout.layout_one),
BLUE(R.string.two, R.layout.layout_two),
GREEN(R.string.three, R.layout.layout_three)
}
- Create a Pager adapter to inflate view CustomPagerAdapter.kt
package com.cretlabs.viewpager /** * Created by Gokul on 2/11/2018. */ import android.content.Context import android.support.v4.view.PagerAdapter import android.view.LayoutInflater import android.view.View import android.view.ViewGroup class CustomPagerAdapter(private val mContext: Context) : PagerAdapter() { override fun instantiateItem(collection: ViewGroup, position: Int): Any { val modelObject = Model.values()[position] val inflater = LayoutInflater.from(mContext) val layout = inflater.inflate(modelObject.layoutResId, collection, false) as ViewGroup collection.addView(layout) return layout } override fun destroyItem(collection: ViewGroup, position: Int, view: Any) { collection.removeView(view as View) } override fun getCount(): Int { return Model.values().size } override fun isViewFromObject(view: View, `object`: Any): Boolean { return view === `object` } override fun getPageTitle(position: Int): CharSequence { val customPagerEnum = Model.values()[position] return mContext.getString(customPagerEnum.titleResId) } }
- Create layout_one.xml to infalte first view
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
style="@style/Base.TextAppearance.AppCompat.Subhead"
android:text="@string/one" />
</android.support.constraint.ConstraintLayout>
- Create layout_two.xml to inflate second view
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
style="@style/Base.TextAppearance.AppCompat.Subhead"
android:text="@string/two" />
</android.support.constraint.ConstraintLayout>
- Create layout_three.xml to inflate third view
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
style="@style/Base.TextAppearance.AppCompat.Subhead"
android:text="@string/three" />
</android.support.constraint.ConstraintLayout>
- Create a activity_main.xml contains viewpager
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.cretlabs.viewpager.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.constraint.ConstraintLayout>
- Create MainActivity.Kt to initialize and setting adapter to Viewpager
package com.cretlabs.viewpager import android.os.Bundle import android.support.v4.view.ViewPager import android.support.v7.app.AppCompatActivity import android.view.View class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main); val viewPager = findViewById<View>(R.id.viewpager) as ViewPager viewPager.adapter = CustomPagerAdapter(this) } }