Allow your WebView pull-to-refresh in Android.
Here is an example:
Insert the following dependency to build.gradle
file of your project.
dependencies {
compile 'com.microwu.ptrwebviewlibray:ptrwebview:1.0.0'
}
Note that Maven central repository should be defined eg. in top-level build.gradle
like this:
buildscript {
repositories {
mavenCentral()
}
}
allprojects {
repositories {
mavenCentral()
}
}
<dependency>
<groupId>com.microwu.ptrwebviewlibray</groupId>
<artifactId>ptrwebview</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
Sample project is under construction. Not all features are covered yet.
===
Just TWO simple steps to create a WebView who can pull-to-refresh.
-
Container in XML file Just create an empty
RelativeLayout
in your activity or view's layout.xml as container.<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/web_view_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
-
Init an instance Create the instance of
PullToRefreshWebView
and add it into theweb_view_container
you created in the first step.@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // NEW THE INSTANCE PullToRefreshWebView ptr_web_view_ = new PullToRefreshWebView(this,null); RelativeLayout container = (RelativeLayout) findViewById(R.id.web_view_container); container.addView(ptr_web_view_); ptr_web_view_.loadUrl("http://www.microwu.com"); }
Note:
PullToRefreshWebView(Context aContext, AttributeSet attrs)
is the constructor method to create an instance,aContext
is current activity's context,attrs
is for webview's attributesSet, you can leave it asnull
.
There are several methods:
- loadUrl(String url) - let webView load the url
- canGoBack() - return webView can go back or not
- goback() - if canGoBack() return true, you can invoke this to go back.
关于源码的中文讲解,请参见这里