I am using ViewPager
with GridView
and databinding
.
The code is like the following:
private fun initGridView(){
//page count
val totalPageSize = ceil(viewModel.rechargePaymentList.value!!.size.toFloat() / 6).toInt()
val viewPagerList = ArrayList<GridView>()
val layoutInflater = LayoutInflater.from(context)
//GridView
for (page in 0 until totalPageSize){
layoutInflater.inflate(R.layout.layout_gridview,binding.viewPager,false).apply {
val gridView = findViewById<GridView>(R.id.gridView)
gridView.adapter = GridViewAdapter(viewModel,context!!,page)
viewPagerList.add(gridView)
}
}
binding.viewPager.adapter = RechargeWayViewPagerAdapter(viewPagerList)
}
Adapter is like the following
class RechargeWayViewPagerAdapter(private val viewList:ArrayList<GridView>): PagerAdapter() {
override fun instantiateItem(container: ViewGroup, position: Int): GridView {
container.addView(viewList[position])
return viewList[position]
}
override fun isViewFromObject(view: View, any: Any): Boolean {
return view == any as GridView
}
override fun getCount(): Int {
return viewList.size
}
override fun destroyItem(container: ViewGroup, position: Int, any: Any) {
container.removeView(viewList[position])
}
}
But it show
IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first
I have try container.removeView(viewList[position])
in override fun instantiateItem
.
But it seems not working.
Did I missing something ? Thanks in advance.