0

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!

Ajith Selvan
  • 45
  • 1
  • 9
  • 1
    "I followed this" -- that is from four years ago and uses `ViewPager`, not `ViewPager2`. "Did I made any mistake" -- [`ViewPager2` does not use a `PagerAdapter`](https://developer.android.com/training/animation/vp2-migration). – CommonsWare Aug 22 '20 at 17:46
  • Thank you , what should I extend or anything I should do . I'm new here. – Ajith Selvan Aug 22 '20 at 18:01
  • "I'm new here" -- then you might want to use `ViewPager` instead of `ViewPager2`, so you can more closely follow the instructions in the answer that you chose. Or, learn how to use `RecyclerView`, after which you will know how to create a `RecyclerView.Adapter` that you can use with `ViewPager2`. – CommonsWare Aug 22 '20 at 18:14

0 Answers0