How to solve for viewpager : The specified child already has a parent. You must call removeView() on the child's parent first

后端 未结 8 1639
半阙折子戏
半阙折子戏 2020-12-04 18:05

Into my project I am using viewpager with three tabs named History,Main,Map.Main activity contain Timer,stopwatch,etc

8条回答
  •  鱼传尺愫
    2020-12-04 18:36

    @Override
    public Object instantiateItem(View arg0, int arg1) {
        Log.d("instantiateItem", ""+arg0+" "+arg1);
        try { 
            if(mListViews.get(arg1).getParent()==null)
                ((ViewPager) arg0).addView(mListViews.get(arg1), 0);  
            else{
                // I am new to android, it is strange that the view to be added is already bound to a parent
                // Through trials and error I solve this problem with the following codes
                // Add that the element of mlistviews is listview in pagerview;
                ((ViewGroup)mListViews.get(arg1).getParent()).removeView(mListViews.get(arg1));
    
                ((ViewPager) arg0).addView(mListViews.get(arg1), 0); 
            }
        } catch (Exception e) {   
            Log.d("parent=", ""+mListViews.get(arg1).getParent()); 
            e.printStackTrace();  
        }  
        return mListViews.get(arg1);
    }
    

提交回复
热议问题