首先介绍一下背景:
我在滚动视图里有一个布局。首先,当用户在屏幕上滚动时,scrollview 会滚动。然而,在一定数量的滚动后,我是禁用滚动视图上的滚动,将“滚动焦点”移动到子布局中的一个 webview 上。通过这种方式,scrollview 粘贴并且所有的滚动事件都进入其中的 webview。
因此,对于解决方案,当达到滚动阈值时,我从 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
After: 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。