首先是一点背景:
我在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.