Quantcast
Channel: Yudiz Solutions Ltd.
Viewing all articles
Browse latest Browse all 595

Kotlin : Android’s future is here – Part 6

$
0
0

Overview

View Pager

This blog is about implementing view pager in Android using Kotlin.
Most of the implementation of view pager in Kotlin is just same as that of in Java.

View Pager without fragments

Including view pager in xml

<android.support.v4.view.ViewPager
   android:id="@+id/view_pager"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />

Row/Page file of View Pager

Here, we have a text view in center of page which will display the page number as we swipe the view pager

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

   <TextView
       android:textSize="30dp"
       android:gravity="center"
       android:text="1"
       android:id="@+id/pageFile_tv"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />
</LinearLayout>

Implementing Pager Adapter

class CustomAdapter(private val con: Context) : PagerAdapter() {

   private val layoutInflater: LayoutInflater

   init {
       layoutInflater = LayoutInflater.from(con)

   }

   override fun getCount(): Int {
       return 5
   }

   override fun isViewFromObject(view: View, `object`: Any): Boolean {
       return view === `object`
   }

   override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
       container.removeView(`object` as View)
   }

   override fun instantiateItem(container: ViewGroup, position: Int): Any {
       val view = layoutInflater.inflate(R.layout.page_file, container, false)
       view.pageFile_tv.text = "${position + 1}"
       container.addView(view)
       return view
   }
}

The name of this adapter is CustomAdapter. It works exactly in same way as that written in Java. There is just a change of language.

Assigning adapter to View Pager in MainActivity

class MainActivity : AppCompatActivity() {

   private var customAdapter: CustomAdapter? = null

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)
       init()
       setAdapter()

   }

   internal fun init() {
       customAdapter = CustomAdapter(baseContext)
   }

   internal fun setAdapter() {
       view_pager!!.adapter = customAdapter
   }
}

Note:- internal keyword behaves like default in java. It is a good practice to assign internal to functions requiring no access modifier as by default they will be final.

View Pager with fragments

Including view pager in xml

<android.support.v4.view.ViewPager
   android:id="@+id/actMain_viewPager"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>

Create the fragments that you want to display inside the view pager

Here, we will display different colored background for each fragment in view pager.
I’ll show one fragment here, for instance.

XML file:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/colorAccent"
   android:orientation="vertical">

   <FrameLayout
       android:id="@+id/frag1_frame"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />
</LinearLayout>

Kotlin file:-

class Frag1 : android.support.v4.app.Fragment() {

   override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
       val view = inflater!!.inflate(R.layout.frag1_layout, container, false)
       return view
   }
}

Implementing FragmentStatePager Adapter

class CustomPageAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {

   override fun getItem(position: Int): android.support.v4.app.Fragment {

       var fragment: android.support.v4.app.Fragment? = null
       when (position) {
           0 -> fragment = Frag1()
           1 -> fragment = Frag2()
       }

       return fragment!!
   }

   override fun getCount(): Int {
       return 2           //no of cases/fragments
   }

}

Assigning adapter to View Pager in MainActivity

class MainActivity : AppCompatActivity() {

   private var customPageAdapter: CustomPageAdapter? = null

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)

       init()
       setAdapter()
   }

   internal fun init() {
       customPageAdapter = CustomPageAdapter(supportFragmentManager)

   }

   internal fun setAdapter() {
       actMain_viewPager!!.adapter = customPageAdapter
   }
}

Conclusion

View Pagers are necessary for the apps where tab layouts are used. They simplify the interface making it user friendly. Coding view pagers in Kotlin is not much different than in Java.


Viewing all articles
Browse latest Browse all 595

Trending Articles