【问题标题】:How to properly implement NestedScrollingChild on a WebView如何在 WebView 上正确实现 NestedScrollingChild
【发布时间】:2016-02-19 16:36:34
【问题描述】:

支持库提供的NestedScrollView 不能与WebView(相关bug report)一起正常工作。因此,我已经着手为WebView 实现NestedScrollingChild,它似乎对第一个滚动、折叠和展开AppBarLayout 工作正常,但任何后续滚动都无法折叠它。

我的WebView

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.NestedScrollingChildHelper;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebView;

/**
 * Created by carlos on 2/19/16.
 */
public class MyWebView extends WebView implements NestedScrollingChild {
    private static final String TAG = MyWebView.class.getSimpleName();
    private NestedScrollingChildHelper helper = new NestedScrollingChildHelper(this);

    public MyWebView(Context context) {
        super(context);
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MyWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public MyWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
        super(context, attrs, defStyleAttr, privateBrowsing);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {

        int dx = l - oldl;
        int dy = t - oldt;
        if (dy != 0) helper.startNestedScroll(View.SCROLL_AXIS_VERTICAL);
        helper.dispatchNestedPreScroll(dx, dy, null, null);
        if (dy != 0) helper.stopNestedScroll();

        super.onScrollChanged(l, t, oldl, oldt);
    }


    @Override
    public boolean isNestedScrollingEnabled() {
        return helper.isNestedScrollingEnabled();
    }

    @Override
    public void setNestedScrollingEnabled(boolean enabled) {
        helper.setNestedScrollingEnabled(enabled);
    }

    @Override
    public boolean startNestedScroll(int axes) {
        return helper.startNestedScroll(axes);
    }

    @Override
    public void stopNestedScroll() {
        helper.stopNestedScroll();
    }

    @Override
    public boolean hasNestedScrollingParent() {
        return helper.hasNestedScrollingParent();
    }

    @Override
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) {
        return helper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
        return helper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
        return helper.dispatchNestedFling(velocityX, velocityY, consumed);
    }

    @Override
    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
        return helper.dispatchNestedPreFling(velocityX, velocityY);
    }
}

我的活动:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;

public class ScrollingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        MyWebView view = (MyWebView) findViewById(R.id.webview);
        view.setNestedScrollingEnabled(true);
        view.setWebViewClient(new WebViewClient());
        WebSettings settings = view.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAllowContentAccess(true);
        settings.setAppCacheEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setBuiltInZoomControls(true);

        settings.setAppCacheEnabled(true);
        settings.setAppCachePath(getCacheDir().getAbsolutePath());
        settings.setDatabaseEnabled(true);
        settings.setSupportMultipleWindows(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowContentAccess(true);
        settings.setAllowFileAccess(true);
        settings.setSaveFormData(true);
        // findViewById(R.id.nested).setLayerType(View.LAYER_TYPE_HARDWARE, null);
        // view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        view.loadUrl("http://vimeo.com");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_scrolling, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我的样式.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>

我的尺寸文件:

<resources>
    <dimen name="app_bar_height">180dp</dimen>
    <dimen name="fab_margin">16dp</dimen>
    <dimen name="text_margin">16dp</dimen>
</resources>

我的活动布局 (activity_scrolling.xml):

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
    android:fitsSystemWindows="true"
    tools:context="com.childscrollview.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|snap|enterAlways">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <com.childscrollview.MyWebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        ></com.childscrollview.MyWebView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"/>

</android.support.design.widget.CoordinatorLayout>

那么我的实现有什么问题还是这是一个错误?

谢谢。

编辑:谷歌现在这样说WebView isn't supported

【问题讨论】:

    标签: android webview android-support-library android-collapsingtoolbarlayout android-appbarlayout


    【解决方案1】:

    我测试了很多WebView,它实现了NestedScrollingChild

    但是,唯一正确的实现位于https://github.com/takahirom/webview-in-coordinatorlayout/blob/master/app/src/main/java/com/github/takahirom/webview_in_coodinator_layout/NestedWebView.java

    您无需任何修改即可使用NestedWebView。它工作得非常顺利。

    我也想分享我的 XML 文件。

    web_view_fragment_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/coordinator_layout"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="4dp"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar_layout"
    
                app:layout_scrollFlags="scroll|enterAlways|snap"
    
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary">
    
                <!-- Toolbar -->
                <include layout="@layout/toolbar"/>
    
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
    
        <!-- http://stackoverflow.com/questions/14171471/remove-vertical-padding-from-horizontal-progressbar -->
        <!-- http://stackoverflow.com/questions/32464749/horizontal-progress-bar-is-not-visible-if-placed-above-toolbar-for-android-5 -->
        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:id="@+id/progress_bar"
            android:layout_gravity="top"
            android:layout_marginBottom="0dp"
            android:layout_marginTop="-4dp"
            android:elevation="4dp"
            android:max="100" />
    
        <FrameLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:foreground="?attr/headerShadow"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    一个片段,包含ViewAnimator(多个WebView添加到ViewAnimator和底部导航栏。

    片段被添加到contentFragmeLayout

    web_view_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/web_view_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal" >
    
        <ViewAnimator
            android:id="@+id/web_view_view_animator"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
        />
    
        <LinearLayout
            android:id="@+id/navigation_linear_layout"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="?attr/colorPrimary"
            android:orientation="horizontal" >
            <LinearLayout
                android:id="@+id/left_linear_layout"
                android:layout_width="0dp"
                android:width="0dp"
                android:layout_weight="0.5"
                android:layout_height="match_parent"
                android:gravity="left"
                android:clickable="true"
                android:background="?attr/selectableItemBackground"
                android:theme="@style/Base.ThemeOverlay.AppCompat.Dark" >
                <ImageView
                    android:id="@+id/left_image_view"
                    android:scaleType="center"
                    android:src="@drawable/ic_chevron_left_white_24dp"
                    android:layout_width="48dp"
                    android:layout_height="match_parent" />
            </LinearLayout>
    
            <LinearLayout
                android:id="@+id/right_linear_layout"
                android:layout_width="0dp"
                android:width="0dp"
                android:layout_weight="0.5"
                android:layout_height="match_parent"
                android:gravity="right"
                android:clickable="true"
                android:background="?attr/selectableItemBackground"
                android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
    
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/next_text_view"
                    android:layout_width="0dp"
                    android:width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
    
                    android:gravity="center_vertical|right"
                    android:ellipsize="end"
                    android:textSize="12sp"
                    android:maxLines = "2"
                    android:minLines = "1"
                    android:lineSpacingMultiplier="1.2"/>
    
                <ImageView
                    android:id="@+id/right_image_view"
                    android:scaleType="center"
                    android:src="@drawable/ic_chevron_right_white_24dp"
                    android:layout_width="48dp"
                    android:layout_height="match_parent" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    

    结果如下

    滚动前的结果

    滚动后的结果

    【讨论】:

    • 它是否适用于不断增长的网站?这是我上次尝试这个的问题。我认为 vimeo 是给我带来问题的网站。
    • 我想知道您所说的网站不断增长是什么意思吗?我已经在许多不同的站点上测试了NestedWebView.java 的实现,到目前为止没有发现问题。滚动行为非常流畅和漂亮。
    • 在您触底时为您提供更多数据的网站。他们有一些视频、故事、帖子等列表,当你到达底部时,他们会给你更多。我遇到的问题是那些网站无法检测到底部。
    • 如果你有这样的网站网址,我可以在我的网站上为你测试。
    【解决方案2】:

    我建议您在主 xml 文件中使用片段。您使用 webview 的片段内部可能会解决您的问题。

    【讨论】:

    • 我用NestedScrollView 来做这件事吗?我在哪里放@string/appbar_scrolling_view_behavior?可以举个例子吗?
    • 这并不能解决问题,将视图放在片段中不会改变视图层次结构。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 2015-02-27
    • 2013-07-22
    • 2021-08-17
    • 2011-07-31
    • 1970-01-01
    相关资源
    最近更新 更多