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

后端 未结 8 1645
半阙折子戏
半阙折子戏 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:30

    First, the answer by RB Patel to insert code (shown below), worked like a charm, but as I read about this feature, I realized it was intended as a potential optimization and isn't actually the solution to the bug.

    mViewPager.setOffscreenPageLimit(3);
    

    The Android Developer site says the following about this method:

    setOffscreenPageLimit(int limit) - Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.

    It seems like this just sets a limit on the number of pages cached to the right or left of the current page. In my case this was just a band aid for the problem, yes the error stopped and things worked smoothly, but I still had bad code lurking in my program which simply not being executed. I believe this method was intended to be used as an optimization and not an actual fix to the bug.

    I ran into this problem when trying to overload the instantiateItem() method in a custom class which extends PagerAdapter. My error was thrown after I would navigate back to a tab/page which I had previously visited/instantiated.

    I am new to Android but I'll do my best to explain my solution. The line which throw this error was the following:

    v.addView(child);
    

    If I understand this correctly, it was choking because the child was already instantiated previously and assigned a parent.

    When I would navigate back to a previously visited view/page I was trying to add a view which already had a parent layout view to a new Layout, hence the error.

    Solution: I just checked to see if the child view I was trying to display (instantiate) was already instantiated somewhere else by seeing if it had a parent view set. If the child view had already been created, then I just used the parent of the child view instead of creating a new Layout.

    Here is my code (I apologize if it offends any experts):

    @Override
    public Object instantiateItem(View container, int position) { 
        LinearLayout v = new LinearLayout(mContext);
    
        //the view being loaded (unless it already was then we have to handle it)
        View child = mViews.get(position);
    
        //If the child view was loaded already then it will have a parent view it belongs to
        //We return the parent view instead of adding the child to the LinearLayout we just created and returning that new layout.
        if(child.getParent() != null) {
            View parent = (View) child.getParent();
            ((ViewPager) container).addView(parent);
            return parent;
        }
    
        //first time instantiating a child view/page
        v.addView(child);            
    
        // These lines execute the first time a given page is instantiated
        ((ViewPager) container).addView(v);
    
        return v;
    }
    

提交回复
热议问题