Android 下拉刷新控件SwipeRefreshLayout結合WebView使用

SwipeRefreshLayout 是谷歌官方下拉刷新控件,4.0如下的版本須要用到 android-support-v4.jar包才能用到html

android-support-v4.jar 包下載地址:輸入連接說明java

官網API地址:輸入連接說明android

GitHub Demo下載地址:輸入連接說明git

輸入圖片說明

SwipeRefreshLayout 使用起來是很是簡單的,只須要在能夠滑動的控件外層添加便可,如:WebView、ListView和ScroolView.github

<?xml version="1.0" encoding="utf-8"?>  
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" >  
  
    <android.support.v4.widget.SwipeRefreshLayout  
        android:id="@+id/swipe_container"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent" >  
  
        <WebView  
            android:id="@+id/webview"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"/>  
          
    </android.support.v4.widget.SwipeRefreshLayout>  
  
</FrameLayout>

經常使用方法: void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) 設置刷新監聽器 void setColorSchemeColors(int color1, int color2, int color3, int color4) 設置四種顏色進度條樣式 void setRefreshing(boolean refreshing) 隱藏或顯示進度條 boolean isRefreshing() 判斷進度條是否顯示web

結合WebView使用也挺簡單的,能夠實現一些功能,下拉刷新當前網頁、點擊網頁在當前頁面中瀏覽並顯示SwipeRefreshLayout進度條,總體來講仍是不錯的ide

public class Fragment5 extends Fragment {  
    private View view;  
    public WebView webview;  
    private SwipeRefreshLayout swipeLayout;  
      
    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState) {  
        view = inflater.inflate(R.layout.activity_fragment5, null);  
          
        swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);  
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {  
              
            @Override  
            public void onRefresh() {  
                //從新刷新頁面  
                webview.loadUrl(webview.getUrl());  
            }  
        });  
        swipeLayout.setColorScheme(R.color.holo_blue_bright,  
                R.color.holo_green_light, R.color.holo_orange_light,  
                R.color.holo_red_light);  
          
        webview = (WebView)view.findViewById(R.id.webview);  
          
        webview.loadUrl("http://blog.csdn.net/h7870181");  
        //添加javaScript支持  
        webview.getSettings().setJavaScriptEnabled(true);   
        //取消滾動條  
        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);  
        //觸摸焦點起做用  
        webview.requestFocus();  
        //點擊連接繼續在當前browser中響應   
        webview.setWebViewClient(new WebViewClient(){  
            @Override  
            public boolean shouldOverrideUrlLoading(WebView view, String url) {  
                view.loadUrl(url);         
                return true;         
            }  
        });  
        //設置進度條  
        webview.setWebChromeClient(new WebChromeClient(){  
            @Override  
            public void onProgressChanged(WebView view, int newProgress) {  
                if (newProgress == 100) {  
                    //隱藏進度條  
                    swipeLayout.setRefreshing(false);  
                } else {  
                    if (!swipeLayout.isRefreshing())  
                        swipeLayout.setRefreshing(true);  
                }  
                  
                super.onProgressChanged(view, newProgress);  
            }  
        });  
          
        return view;  
    }  
}

差點忘了貼出color.xml資源文件了,我呵了個呵!url

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
  
    <!-- A light Holo shade of blue -->  
    <color name="holo_blue_light">#ff33b5e5</color>  
    <!-- A light Holo shade of green -->  
    <color name="holo_green_light">#ff99cc00</color>  
    <!-- A light Holo shade of red -->  
    <color name="holo_red_light">#ffff4444</color>  
    <!-- A dark Holo shade of blue -->  
    <color name="holo_blue_dark">#ff0099cc</color>  
    <!-- A dark Holo shade of green -->  
    <color name="holo_green_dark">#ff669900</color>  
    <!-- A dark Holo shade of red -->  
    <color name="holo_red_dark">#ffcc0000</color>  
    <!-- A Holo shade of purple -->  
    <color name="holo_purple">#ffaa66cc</color>  
    <!-- A light Holo shade of orange -->  
    <color name="holo_orange_light">#ffffbb33</color>  
    <!-- A dark Holo shade of orange -->  
    <color name="holo_orange_dark">#ffff8800</color>  
    <!-- A really bright Holo shade of blue -->  
    <color name="holo_blue_bright">#ff00ddff</color>  
  
</resources>
相關文章
相關標籤/搜索