Java 如何在顶部堆叠WebView并将url加载到其中而不向下推视图
在我们的应用程序中,我正在加载一个初始网络视图。然后,为了允许用户查看聊天历史,我想在最初的Web视图上添加新的Web视图。我现在这样做的方式是有一个线性布局包装最初的网络视图;此线性布局称为webview_包装器,位于滚动视图中。然后,使用ScrollViewListener界面,当用户向上滚动超过设置的坐标时,我创建一个新的webview,称之为newWebView,并称之为webview_wrapper.addView(newWebView,0),我遇到的问题是,我想在屏幕外加载和添加webview,然后用户可以继续向上滚动。这种添加和滚动发生在AnimationListener的onAnimationEnd方法内部(当我对webview发出post请求时)Java 如何在顶部堆叠WebView并将url加载到其中而不向下推视图,java,android,android-layout,android-webview,android-scrollview,Java,Android,Android Layout,Android Webview,Android Scrollview,在我们的应用程序中,我正在加载一个初始网络视图。然后,为了允许用户查看聊天历史,我想在最初的Web视图上添加新的Web视图。我现在这样做的方式是有一个线性布局包装最初的网络视图;此线性布局称为webview_包装器,位于滚动视图中。然后,使用ScrollViewListener界面,当用户向上滚动超过设置的坐标时,我创建一个新的webview,称之为newWebView,并称之为webview_wrapper.addView(newWebView,0),我遇到的问题是,我想在屏幕外加载和添加we
我觉得我已经尝试了各种方法,比如在添加视图后调用scrollTo或scrollBy,但它只是部分滚动。最好的方法是什么?这个问题最终通过在包含的ScrollView上使用OnGlobalYoutListener解决了,在其中我调用scrollTo(0,webViewHeight)并移除OnGlobalYoutListener(this)。在此之前,我让WebView可见,通过setLayoutParams设置其高度(视图必须首先可见) 如果我错了,请纠正我,但由于OnGlobalLayoutListener,这可能会像它一样无缝地工作。当视图可见并设置了高度时,在我们设置了OnGlobalLayoutListener(在布置scrollView时将scrollTo()激发到新视图的精确高度)之后,最终结果是视图在屏幕外膨胀
final int newHeight = (int) ((height / 380.0) * webView.getWidth());
// Must set visible before setting layout params
currentOldView.setVisibility(View.VISIBLE);
// Without this line, view will bounce around as it attempts to self set the height based on HTML content
currentOldView.setLayoutParams(new LinearLayout.LayoutParams(webView.getWidth(), newHeight));
// Use viewTreeObserver to wait until scrollView completely laid out before attempting to scroll
ViewTreeObserver observer = scrollView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
scrollView.scrollTo(0, newHeight); // Because spinner_wrapper still on view, will scroll to an overlap point.
canAddOldChat = true;
currentOldView.startAnimation(fadeInAnim);
if (isLastOldChat) spinner_wrapper.setAlpha(0.0f);
scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
这个问题最终通过在包含ScrollView的视图上使用OnGlobalLayoutListener得到解决,在其中我调用scrollTo(0,webViewHeight)。在此之前,我让WebView可见,通过setLayoutParams设置其高度(视图必须首先可见)。如果我错了,请纠正我,但这是有效的,因为