2016-07-16 128 views
0

到目前爲止,通過下面的代碼,我可以在向上滾動webview時顯示工具欄,並在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(); 
    } 
} 

回答

0

嘗試更換SwipeRefreshLayout和位置NestedScrollView片段:

<?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> 

  • 採取appbar_scrolling_view_behavior關閉NestedScrollView(離開它的ViewPager,這就是你需要使它工作)

  • 更改您的WebViewwrap_content的高度。您可能還需要使用SwipeRefreshLayout來做到這一點。

+0

我跟着你的建議,但沒有任何效果可言。我的webview是WebChromeClient。我沒有設置任何關於默認滾動它的屬性。我還使用導航抽屜它是否必須做任何事情? – choman

+0

已更新的答案。 –

+0

刷新刷新終於與代碼工作但是我無法滾動webview作爲佈局顯然使webview的滾動固定。我現在可以隱藏和顯示工具欄/刷新我的webview,但沒有水平滾動。如果它有幫助,我已經更新了包含webview的片段。 – choman

相關問題