【问题标题】:can't enable scroll behaviour of webview when hiding toolbar隐藏工具栏时无法启用 webview 的滚动行为
【发布时间】:2016-07-16 16:23:44
【问题描述】:

到目前为止,使用下面的代码,我可以在向上滚动 web 视图时隐藏工具栏,并在向下滚动 web 视图时显示工具栏,但是通过下面的这个实现,我已经失去了 webview 本身的滚动行为

这是我的 app_bar.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"
    tools:context=".Activity.MainActivity">
    <!--android:fitsSystemWindows="true"-->


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="35dp"
            app:tabGravity="fill"
            app:tabMode="scrollable">

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


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

这是我在片段中使用的 news.xml。我的问题是如果我删除 NestedScrollView 我可以滚动 webview 并执行滑动刷新功能但是当我添加嵌套视图时我可以上下滚动工具栏但是 webview 的滚动属性和滑动刷新是丢了

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.support.v4.widget.NestedScrollView

        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:minHeight="60dp"
        >

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

            </WebView>
        </android.support.v4.widget.SwipeRefreshLayout>
    </android.support.v4.widget.NestedScrollView>
    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:indeterminate="true"
        android:visibility="visible"/>

</RelativeLayout>

包含 webview 的片段

public class NewsFragment extends Fragment {



    // private String url;
    private WebView webView;
    private ProgressBar progressBar1;
    private SwipeRefreshLayout mSwipeRefreshLayout1;

    public NewsFragment() {
        // Required empty public constructor
    }


    public static NewsFragment newInstance(String webUrl) {
        //set arguments
        Bundle args = new Bundle();
        args.putString("website", webUrl);
        NewsFragment newsFragment = new NewsFragment();

        newsFragment.setArguments(args);

        return newsFragment;

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment


        return inflater.inflate(R.layout.test, container, false);


    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        //get back arguments
        final String url = this.getArguments().getString("website");


        progressBar1 = (ProgressBar) view.findViewById(R.id.progressBar1);

        webView = (WebView) view.findViewById(R.id.website_detail_1);
        webView.getSettings().setJavaScriptEnabled(true);




        webView.setWebChromeClient(new WebChromeClient() {


            public void onProgressChanged(WebView view, int progress) {

                progressBar1.setProgress(progress);

                if (progress == 100) {

                    progressBar1.setVisibility(View.GONE);

                    if (mSwipeRefreshLayout1.isRefreshing()) {
                        mSwipeRefreshLayout1.setRefreshing(false);
                    }
                } else {
                    progressBar1.setVisibility(View.VISIBLE);
                }

            }


        });


        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        });


        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        WebSettings webSettings = webView.getSettings();
        //   webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.getSettings().setAppCacheEnabled(true);
        webSettings.setDomStorageEnabled(true);

        if (!isNetworkAvailable()) { // loading offline
            webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }


        webView.loadUrl(url);

        mSwipeRefreshLayout1 = (SwipeRefreshLayout) view.findViewById(R.id.swipe1);
        mSwipeRefreshLayout1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.loadUrl(url);
            }
        });

        webView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
                    webView.goBack();
                    return true;
                }

                return false;
            }
        });

    }


    private boolean isNetworkAvailable() {

        ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        // ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}

【问题讨论】:

    标签: android android-layout webview android-coordinatorlayout


    【解决方案1】:

    我认为它更复杂,但这个存储库帮助了我

    <yourpackage.name.NestedWebView 
        android:id="@id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    https://github.com/takahirom/webview-in-coordinatorlayout/blob/master/app/src/main/java/com/github/takahirom/webview_in_coodinator_layout/NestedWebView.java

    【讨论】:

      【解决方案2】:

      尝试交换SwipeRefreshLayoutNestedScrollView 的位置:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout 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.support.v4.widget.SwipeRefreshLayout
              android:id="@+id/swipe1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
              <android.support.v4.widget.NestedScrollView
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:fillViewport="true">
      
                  <WebView
                      android:id="@+id/website_detail_1"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"/>
      
              </android.support.v4.widget.NestedScrollView>
      
          </android.support.v4.widget.SwipeRefreshLayout>
      
          <ProgressBar
              android:id="@+id/progressBar1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true"
              android:indeterminate="true"
              android:visibility="visible"/>
      
      </RelativeLayout>
      

      • NestedScrollView 中删除appbar_scrolling_view_behavior(将其留在ViewPager 上,这就是让它工作所需的全部内容)

      • WebView 的高度更改为wrap_content。您可能还需要使用SwipeRefreshLayout 执行此操作。

      【讨论】:

      • 我听从了你的建议,但它根本没有任何效果。我的 webview 是 WebChromeClient。我没有设置任何关于在默认情况下滚动它的属性。我也在使用导航抽屉它必须做任何事情吗?
      • 刷卡刷新最终适用于代码但是我无法滚动 webview,因为布局显然使 webview 的滚动固定。我现在可以隐藏和显示工具栏/刷新我的 web 视图,但不能水平滚动。如果有帮助,我已经更新了包含 webview 的片段?
      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多