Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
funnyzhaov opened this issue May 8, 2020 · 0 comments
Open

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

funnyzhaov opened this issue May 8, 2020 · 0 comments

Comments

@funnyzhaov
Copy link

错误
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);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant