【问题标题】:Scrolling issues with webviewwebview的滚动问题
【发布时间】:2019-03-05 20:19:56
【问题描述】:

尝试了太多次但失败了,这是我尝试过的代码

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true">


<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainweb">
</WebView>

 </android.support.v4.widget.NestedScrollView>

在启动应用程序时,嵌套滚动视图工作正常,但有时 webview 不滚动。

我在嵌套滚动视图中插入 webview 以隐藏片段活动中的工具栏

任何帮助将不胜感激,因为我在过去 7 个月中一直被这个问题困扰

【问题讨论】:

标签: android xml android-layout layout webview


【解决方案1】:

WebView 有自己的滚动,所以不需要 NestedScrollView。

因为 NestedScrollView Webview 出现滚动问题。

请尝试以下代码以供参考:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp" />
</RelativeLayout>

您可以根据需要添加 webview 的父级。如 LinearLayout、ConstraintLayout 等。

【讨论】:

    【解决方案2】:

    你需要在 build.gradle 文件中添加这个依赖

    implementation 'com.github.ksoichiro:android-observablescrollview:1.6.0'
    

    在你的 java 类中

    ObservableWebView webView;
    
    webView.setScrollViewCallbacks(new ObservableScrollViewCallbacks() {
            @Override
            public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
    
            }
    
            @Override
            public void onDownMotionEvent() {
    
            }
    
            @Override
            public void onUpOrCancelMotionEvent(ScrollState scrollState) {
    
    
                if (scrollState == ScrollState.UP) {
    
                    ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
                    // hide your action bar
                } else if (scrollState == ScrollState.DOWN) {
                    ((AppCompatActivity)getActivity()).getSupportActionBar().show();
                    // show your action bar
                }
    
    
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2017-05-17
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多