0

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.

Wun
  • 6,211
  • 11
  • 56
  • 101
  • Is the `` the only thing in the `layout_gridview`? If not, then it's inside some other `ViewGroup`, which would be why it already has a parent. – Mike M. Sep 06 '19 at 08:59
  • @MikeM. It also has a RelativeLayout. When I remove it. it work fine! but if it does not has RelativeLayout , how to I center the gridview in viewpage ? – Wun Sep 06 '19 at 09:05
  • You can keep the layout as it is. You just need to add the whole inflated `View` to the `viewPagerList`, instead of just the `GridView`. So change `viewPagerList` to be an `ArrayList`, then `viewPagerList.add(this)` (I think; I'm not up to speed on those little Kotlin functions yet). – Mike M. Sep 06 '19 at 09:08
  • Possible duplicate of [The specified child already has a parent. You must call removeView() on the child's parent first (Android)](https://stackoverflow.com/questions/28071349/the-specified-child-already-has-a-parent-you-must-call-removeview-on-the-chil) – a_local_nobody Sep 06 '19 at 10:02

1 Answers1

0

From the issue, your childView already has a parent so do something as below.

Assuming that container.addView(viewList[position]) line is culprit then childview will be View childView = viewList[position]

if (childView != null) {
       ViewGroup parent = (ViewGroup) childView.getParent();
       if (parent != null) {
           parent.removeView(childView);
       }
}
Bali
  • 749
  • 6
  • 19