【问题标题】:Load webview in the top of the page在页面顶部加载 webview
【发布时间】:2017-07-23 22:22:12
【问题描述】:

我正在使用 WebView 应用程序。 在我的最后一次更改中,我添加了一个功能,当我在 webview 中向下滚动时隐藏应用栏。好的 !!! 但是我注意到了一个问题。我会尝试解释。 假设我在一个新闻站点中,并且我正在打开最后一条新闻。最近的新闻在页面的开头,最后的新闻接近页面的结尾,所以我打开最后的新闻,我的 webview 加载 url。加载 url 时,我的 webview 在站点的末尾,而不是在开头。它是在我在“NestedScrollView”中添加我的“webview”之后开始的。看起来 NestedScrollView 正在保存我的位置,它影响了我在网页中的位置。如何解决这个问题?

编辑:我的 webview 在 Framelayout 中加载,我的 webview 是一个片段。

WebView 布局

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipelayout">

    <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        >

<WebView
    android:id="@+id/inicio_pagina"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"></WebView>

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

框架布局

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">


        <FrameLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></FrameLayout>

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

App_Bar_Main

<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.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

<include layout="@layout/content_main"
    android:id="@+id/include" />


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/face"
    android:scaleType="center"/>

编辑 2:WebView 片段

public class InicioPagina extends Fragment {

private ProgressBar prg;

SwipeRefreshLayout mySwipeRefreshLayout;

NestedScrollView NestedScrollView;

public WebView myWebView;

public static InicioPagina newInstance() {
    InicioPagina fragment = new InicioPagina();
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.iniciopagina, container, false);
    prg = (ProgressBar)rootView.findViewById(R.id.progressBar2);
    NestedScrollView = (NestedScrollView) rootView.findViewById(R.id.nested_scroll_view);
    mySwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipelayout);
    myWebView = (WebView) rootView.findViewById(R.id.inicio_pagina);
    myWebView.loadUrl("http://google.com.br/");

    //*Ativar JavaScript
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    //*Forçar links para abrir no WebView ao invés do navegador
    myWebView.setWebViewClient(new WebViewClient());
    NestedScrollView.scrollTo(0, 0);


    myWebView.setVerticalScrollBarEnabled(false);

    mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            myWebView.reload();
            mySwipeRefreshLayout.setRefreshing(false);
        }
    });

    myWebView.setOnKeyListener(new View.OnKeyListener()
    {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if(event.getAction() == KeyEvent.ACTION_DOWN)
            {
                WebView webView = (WebView) v;

                switch(keyCode)
                {
                    case KeyEvent.KEYCODE_BACK:
                        if(webView.canGoBack())
                        {
                            webView.goBack();
                            return true;
                        }
                        break;
                }
            }

            return false;
        }
    });

    return rootView;
}

public class WebViewClient extends android.webkit.WebViewClient{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        prg.setVisibility(View.VISIBLE);
        super.onPageStarted(view, url, favicon);
    }
    @Override
    public void onPageFinished(WebView view, String url) {
        prg.setVisibility(View.GONE);

        super.onPageFinished(view, url);
    }
}

【问题讨论】:

    标签: android webview


    【解决方案1】:

    1) 将id 设为NestedScrollView

     <android.support.v4.widget.NestedScrollView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/nested_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical">
    


    2) 创建NestedScrollView 对象
    3) 当新的 URL 加载完毕后,将
    nestedScrollViewobject 的滚动属性设置为 0,0。

    nestedScrollViewObj.scrollTo(0, 0);
    

    【讨论】:

    • 不工作。我不知道我做的对不对,所以我用 WebView Fragment 更新了帖子,请看一下
    • 首先确保页面加载完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多