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

kas*_*oku 135 android scrollview

首先是一点背景:

我在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);
Run Code Online (Sandbox Code Playgroud)

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

之前: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)
Run Code Online (Sandbox Code Playgroud)

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

kas*_*oku 287

解:

((ViewGroup)scrollChildLayout.getParent()).removeView(scrollChildLayout);
//scrollView.removeView(scrollChildLayout);
Run Code Online (Sandbox Code Playgroud)

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

感谢@Dongshengcn的解决方案


don*_*gcn 21

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

scrollview.removeView(scrollChildLayout)
Run Code Online (Sandbox Code Playgroud)

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

scrollview.removeAllViews()
Run Code Online (Sandbox Code Playgroud)

  • 您可以通过查看以下内容来查看其父级:view.getParent()http://developer.android.com/reference/android/view/View.html#getParent() (4认同)

Cab*_*zas 19

在带有活动的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) {

}
Run Code Online (Sandbox Code Playgroud)


Mar*_*tin 16

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

  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
Run Code Online (Sandbox Code Playgroud)

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


Gib*_*olt 5

科特林解决方案

Kotlin 使用 简化了父级转换as?,如果左侧为 null 或转换失败,则返回 null。

(childView.parent as? ViewGroup)?.removeView(childView)
Run Code Online (Sandbox Code Playgroud)

Kotlin 扩展解决方案

如果您想进一步简化,可以添加此扩展。

childView.removeSelf()

fun View?.removeSelf() {
    this ?: return
    val parentView = parent as? ViewGroup ?: return
    parentView.removeView(this)
}
Run Code Online (Sandbox Code Playgroud)

如果此 View 为 null、父视图为 null 或父视图不是 ViewGroup,它将安全地不执行任何操作

注意:如果您还希望父视图安全地删除子视图,请添加以下内容:

fun ViewGroup.removeViewSafe(toRemove: View) {
    if (contains(toRemove)) removeView(toRemove)
}
Run Code Online (Sandbox Code Playgroud)