Android 下拉重新整理控制項SwipeRefreshLayout結合WebView使用

來源:互聯網
上載者:User

標籤:控制項   webview   android   

SwipeRefreshLayout 是Google官方下拉重新整理控制項,4.0以下的版本需要用到  android-support-v4.jar包才能用到

android-support-v4.jar 包:http://download.csdn.net/detail/h7870181/7784247

官網API地址:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

GitHub Demo: https://github.com/stormzhang/SwipeRefreshLayoutDemo



SwipeRefreshLayout 使用起來是非常簡單的,只需要在可以滑動的控制項外層添加即可,如:WebView、ListView和ScroolView.

<?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()  判斷進度條是否顯示 


結合WebView使用也挺簡單的,可以實現一些功能,下拉重新整理當前網頁、點擊網頁在當前頁面中瀏覽並顯示SwipeRefreshLayout進度條,整體來說還是不錯的

public class Fragment5 extends Fragment {private View view;public WebView webview;private SwipeRefreshLayout swipeLayout;@Overridepublic 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() {@Overridepublic 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(){@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);                       return true;       }});//設定進度條webview.setWebChromeClient(new WebChromeClient(){@Overridepublic 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資源檔了,我呵了個呵!

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


沒多大技術含量,純屬積累學習,還望大家見諒!


成功源於不斷的學習和積累 !


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.