首页 文章

首先在孩子的父母上调用removeView()

提问于
浏览
130

首先是一点背景:

我在scrollview中有一个布局 . 首先,当用户在屏幕上滚动时,滚动视图滚动 . 但是,经过一定量的滚动后,我要禁用滚动视图上的滚动,将“滚动焦点”移动到子布局内的webview上 . 这样,scrollview会将所有滚动事件都粘贴到其中的webview中 .

因此,对于解决方案,当达到滚动阈值时,我从scrollview中删除子布局并将其放在scrollview的父级中 . (并使scrollview不可见) .

// Remove the child view from the scroll view
scrollView.removeView(scrollChildLayout);

// Get scroll view out of the way
scrollView.setVisibility(View.GONE);

// Put the child view into scrollview's parent view
parentLayout.addView(scrollChildLayout);

一般理念:( - >表示包含)

之前:parentlayout - > scrollview - > scrollChildLayout

之后:parentLayout - > scrollChildLayout

上面的代码给了我这个例外:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
           at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
           at android.view.ViewGroup.addView(ViewGroup.java:1871)
           at android.view.ViewGroup.addView(ViewGroup.java:1828)
           at android.view.ViewGroup.addView(ViewGroup.java:1808)

你知道发生了什么吗?我显然在父级上调用了removeView .

10 回答

  • 0

    这是我的解决方案 .

    假设你有两个 TextViews 并将它们放在 LinearLayout (名为 ll )上 . 你会把这个 LinerLayout 放在另一个 LinerLayout 上 .

    < lm Linear Layout> 
           < ll Linear Layout> 
                 <name Text view>
                 </name Text view>
                 <surname Text view>
                 </surname Text view>
           </ll Linear Layout> 
    </lm Linear Layout>
    

    如果要创建此结构,则需要将父级作为继承 .

    如果你想在 onCreate 方法中使用 this 就足够了 .

    否则这里是solition:

    LinerLayout lm = new LinearLayout(this); // You can use getApplicationContext() also
    LinerLayout ll = new LinearLayout(lm.getContext());
    TextView name = new TextView(ll.getContext());
    TextView surname = new TextView(ll.getContext());
    
  • 0

    解:

    ((ViewGroup)scrollChildLayout.getParent()).removeView(scrollChildLayout);
    //scrollView.removeView(scrollChildLayout);
    

    使用child元素获取对父元素的引用 . 将父级转换为ViewGroup,以便您可以访问removeView方法并使用它 .

    感谢@Dongshengcn的解决方案

  • 0

    尝试首先从其父视图中删除scrollChildLayout?

    scrollview.removeView(scrollChildLayout)
    

    或者从父视图中删除所有子项,然后重新添加它们 .

    scrollview.removeAllViews()
    
  • 1

    在带有活动的onCreate或带有片段的onCreateView中 .

    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
    }
    try {
        view = inflater.inflate(R.layout.fragment_main, container, false);
    } catch (InflateException e) {
    
    }
    
  • 3

    好吧,叫我偏执,但我建议:

    final android.view.ViewParent parent = view.getParent ();
    
      if (parent instanceof android.view.ViewManager)
      {
         final android.view.ViewManager viewManager = (android.view.ViewManager) parent;
    
         viewManager.removeView (view);
      } // if
    

    没有 instanceof 的铸造似乎错了 . 并且(感谢IntelliJ IDEA告诉我) removeViewViewManager 界面的一部分 . 当一个完全合适的界面可用时,不应该转向一个具体的类 .

  • 19

    你所要做的就是post()一个执行addView()的Runnable .

  • 16

    我正在调用parentView.removeView(childView)并且仍在显示childView . 我终于意识到一个方法以某种方式被触发两次并将childView添加到parentView两次 .

    因此,使用parentView.getChildCount()来确定父项在添加视图之前和之后的子项数 . 如果孩子被添加太多次,那么最顶层的孩子将被移除并且复制childView仍然存在 - 看起来即使它正在工作,removeView也在工作 .

    此外,您不应使用View.GONE删除视图 . 如果真的被删除了,那么你就不需要隐藏它了,否则它仍然在那里而你只是将它隐藏起来:(

  • 22

    在我的情况下,我有BaseFragment和所有其他片段继承自此 .

    所以我的意见是在 OnDestroyView() 方法中添加这一行

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        if (mRootView == null)
        {
            mRootView = (inflater == null ? getActivity().getLayoutInflater() : inflater).inflate(mContentViewResourceId, container, false);
        }
    ....////
    }
    
    @Override
    public void onDestroyView()
    {
        if (mRootView != null)
        {
            ViewGroup parentViewGroup = (ViewGroup) mRootView.getParent();
    
            if (parentViewGroup != null)
            {
                parentViewGroup.removeAllViews();
            }
        }
    
        super.onDestroyView();
    }
    
  • 280

    你也可以通过检查View的indexOfView方法,如果indexOfView方法返回-1然后我们可以使用 .

    ViewGroup的detachViewFromParent(v);其次是ViewGroup的removeDetachedView(v,true / false);

  • 1

    我做错了所以我得到这个错误是我没有实例化动态布局并添加子项,所以得到了这个错误

相关问题