I have been trying to set gallery like view so I created a class which extends PagerAdapter as like in the link. I followed this
But my problem is I can't setAdapter to viewPager as it asks RecycleView.Adapter<RecyclerView.ViewHolder!> as type but I got PageAdaper(name of the class which extends PagerAdapter) as type.
Val iAdapter=PagerAdapter(this,galleryUri) //creating object
viewPager.adapter=iAdapter // type mismatched here
But on the code on the link has no issues like that and everyone done like that . Did I made any mistake.
PageAdapter.kt
class PageAdapter(context: Context, arrayList: ArrayList<Uri>) : PagerAdapter() {
private var mContext: Context? =null
private var galleryUri= arrayListOf<Uri>()
private var layoutInflater: LayoutInflater? =null
init {
this.mContext=context
this.galleryUri=arrayList
this.layoutInflater=context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
}
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view==`object` as LinearLayout
}
override fun getCount(): Int {
return galleryUri.size
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val itemView=(layoutInflater?.inflate(R.layout.image_container,container,true))
val imageView= itemView?.findViewById<ImageView>(R.id.imageContainer)
imageView?.setImageURI(galleryUri[position])
container.addView(itemView)
return itemView as Any
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as LinearLayout)
}
}
Thank you!