luckybilly/Gloading

fragment+viewpager,在fragment中使用,会报错:

funnyzhaov opened this issue · 0 comments

错误
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

出错抛异常源码:
ViewGroup

  if (child.getParent() != null) {
            throw new IllegalStateException("The specified child already has a parent. " +
                    "You must call removeView() on the child's parent first.");
        }

使用场景

创建Fragment页面与viewpager关联

        private void createTypeFragment(List<SearchTypeBean> searchTypeBeanList){

        SearchTypeBean beanAll=new SearchTypeBean();
        beanAll.setAction(0);
        beanAll.setTitle(getResources().getString(R.string.search_all_btn));

        searchTypeBeanList.add(0,beanAll);
        mPagerAdapter.refreshFragments(searchTypeBeanList,mKeyword);
        mTypeVp.setOffscreenPageLimit(searchTypeBeanList.size()-1);
        mTypeVp.setAdapter(mPagerAdapter);
        mTypeTabLayout.setupWithViewPager(mTypeVp);

    }

Fragment加载数据,先展示loading,再关闭

推测原因

看代码应该是这段代码出的问题,求解

   public Holder wrap(View view) {
        FrameLayout wrapper = new FrameLayout(view.getContext());
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp != null) {
            wrapper.setLayoutParams(lp);
        }
        if (view.getParent() != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            int index = parent.indexOfChild(view);
            parent.removeView(view);
            parent.addView(wrapper, index);
        }
        LayoutParams newLp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        wrapper.addView(view, newLp);
        return new Holder(mAdapter, view.getContext(), wrapper);
    }