Kotlin Create Slider With Page Indicator Using ViewPager Example
Last updated Jan 22, 2020Kotlin Create Slider With Page Indicator Using ViewPager Example
Viewpager we can do a lot of things, from the simplest navigation, to the page menu and so on. Then how to use it, similar to ListView, we also need an adapter, which is PagerAdapter
In this post we are going to see page indicator on slider that are created using viewpager
Kotlin Create Slider With Page Indicator Using ViewPager Example
Step 1:Creat application in Android Stuido
Step 2: Create a model class which contains Slider item
import java.io.Serializable class ViewItem : Serializable { constructor(id: Int, drawable: Int) { |
Step 3: Create Pager Adapter
class ViewPagerAdapter(internal var context: Context, internal var itemList: List<ViewItem>) : PagerAdapter() { internal var mLayoutInflater: LayoutInflater init { override fun getCount(): Int { override fun instantiateItem(container: ViewGroup, position: Int): Any { if (pos > this.itemList.size - 1) holder.sliderItem = this.itemList[pos] (container as ViewPager).addView(itemView) holder.itemImage.scaleType = ImageView.ScaleType.FIT_XY pos++ internal class ViewHolder { override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) { override fun isViewFromObject(arg0: View, arg1: Any): Boolean { override fun destroyItem(arg0: View, arg1: Int, arg2: Any) { |
ViewPager adapter is PagerAdapter, which is the base class that provides the adapter to fill the page ViewPager's internals. You may want to use a more specific implementation, such as FragmentPagerAdapter or FragmentStatePagerAdapter. It needs to be explained here, in fact, ViewPager should be used with Fragment, at least Google officially thinks so, but under 3.0, we do not need to do so. Note that when you implement a PagerAdapter, you must at least override the following methods:
- instantiateItem(ViewGroup, int)
- destroyItem(ViewGroup, int, Object)
- getCount()
- isViewFromObject(View, Object)
Step 4:
Add Adapter view item layout_item_view_pager.xml
<?xml version="1.0" encoding="utf-8"?> <ImageView </LinearLayout> |
Step 5:
Update activity_main.xml with Viewpager and page indicator layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout <androidx.viewpager.widget.ViewPager <LinearLayout </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout> |
Step 6:
Addrequired Resourses to the application
Step 7:
Add Adapter to Viewpager
fun initUI() adapter= ViewPagerAdapter(applicationContext,listViews) |
Step 8: Add PageIndicators to page
fun addPageIndicators() lyt_page_indicator.addView(view) |
Step 9:
Update PageIndicator on Page Selection
fun updatePageIndicator(position: Int) { val lp = |
Complete MainActivity code
package com.rrtutors.kotlinviewpager import androidx.appcompat.app.AppCompatActivity
lateinit var listViews:ArrayList<ViewItem> fun initUI() adapter= ViewPagerAdapter(applicationContext,listViews) } override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { } override fun onPageSelected(position: Int) { fun addPageIndicators() lyt_page_indicator.addView(view) val lp = |
Article Contributed By :
|
|
|
|
4785 Views |