Android 带可折叠工具栏的WebView?(材料设计)
我正在开发一个Android应用程序,其中包含一个活动和两种片段 类型1:具有可折叠工具栏和作为内容的RecyclerView的片段。(它起作用了)。 布局: 我也试过:Android 带可折叠工具栏的WebView?(材料设计),android,android-layout,android-fragments,webview,Android,Android Layout,Android Fragments,Webview,我正在开发一个Android应用程序,其中包含一个活动和两种片段 类型1:具有可折叠工具栏和作为内容的RecyclerView的片段。(它起作用了)。 布局: 我也试过: WebSettings ws = webView.getSettings(); ws.setJavaScriptEnabled(true); webView.loadUrl("http://www.google.com"); webView.setWebViewClient(new WebViewC
WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
将您的Webview放入NestedScrollView并设置android:isScrollContainer:false 应该如此
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<WebView
android:id="@+id/scrollableview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:isScrollContainer="false"/>
</android.support.v4.widget.NestedScrollView>
您可以收听scroll事件以产生折叠效果,而不是WebView 布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="146dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="@color/primary"
app:expandedTitleGravity="bottom"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
代码
webview.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
// i1 last Y, i3 previous Y
// Do you stuff here. A simple example: (not performant)
AppBarLayout appBarLayout =
CollapsingToolbarActivity.this.findViewById(R.id.appBarLayout);
appBarLayout.setExpanded(i1 < i3, true);
}
});
webview.setOnScrollChangeListener(新视图.OnScrollChangeListener(){
@凌驾
公共void onScrollChange(视图视图、int i、int i1、int i2、int i3){
//i1上一个Y,i3上一个Y
//你喜欢这里的东西吗?一个简单的例子:(notperformant)
AppBarLayout AppBarLayout=
折叠ToolBarActivity.this.findViewById(R.id.appBarLayout);
appBarLayout.setExpanded(i1
仍然不可见,我将编辑我的问题,向您展示如何加载webView。感谢您的帮助。NestedScrollView有效,无需或android:isScrollContainer:“false”。永远不要将webview放在另一个滚动视图中以避免高度问题。人们是如何理解这些问题的?NestedScrollView中的XDWebView可能会产生一些奇怪的滚动问题,以及js小程序的问题。认真
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<WebView
android:id="@+id/scrollableview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:isScrollContainer="false"/>
</android.support.v4.widget.NestedScrollView>
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="146dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="@color/primary"
app:expandedTitleGravity="bottom"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
webview.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
// i1 last Y, i3 previous Y
// Do you stuff here. A simple example: (not performant)
AppBarLayout appBarLayout =
CollapsingToolbarActivity.this.findViewById(R.id.appBarLayout);
appBarLayout.setExpanded(i1 < i3, true);
}
});