How to implement pull down refresh in android?
Solution 1
Check out the awesome ActionBar-PullToRefresh Library. It does the pull to refresh gesture using the gmail/ google+ progress bar, is highly configurable, very easy to implement.
Solution 2
For those who wants to use this feature in webView!
In your xml layout, wrap your WebView
into SwipeRefreshLayout
:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
In your Activity/Fragment class:
//declare it as global var for future cancel refresh
private SwipeRefreshLayout swipeLayout;
//where you initialize your views:
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//your method to refresh content
}
});
//don't forget to cancel refresh when work is done
if(swipeLayout.isRefreshing()) {
swipeLayout.setRefreshing(false);
}
for example you can set up a webView client and when page is loaded you'll cancel refresh:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView web, String url) {
if (swipeLayout.isRefreshing()) {
swipeLayout.setRefreshing(false);
}
}
});
Solution 3
You can use SwipeRefreshLayout available in android support library.
A complete sample code example with tutorial is avaialable at this link
Comments
-
user782104 almost 2 years
Currently I am working on a fragment that simply a webview with framelayout, the problem is, I would like to do something like the pull down refresh ( just like some refresh function common of list view ).
Assume there is a
refreshToDo()
function , all I need is just a layout (when I drag the body it show the refresh header, when the header at the certain height, it callrefreshToDo()
, when I release it , it go back to top and hide) , but how to implement that? thankslayout: (It contains the main content and target content, you can simply ingore target content, it is for playing the video in fullscreen in webview):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <FrameLayout android:id="@+id/main_content" 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" /> </FrameLayout> <FrameLayout android:id="@+id/target_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:visibility="gone" > </FrameLayout> </RelativeLayout>
Fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.web_fragment, container, false); mTargetView = (FrameLayout) rootView.findViewById(R.id.target_view); mContentView = (FrameLayout) rootView.findViewById(R.id.main_content); mWebView = (WebView) rootView.findViewById(R.id.webView); mWebView.setVerticalScrollBarEnabled(false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { WebView.setWebContentsDebuggingEnabled(true); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true); mWebView.setWebViewClient(new MyWebViewClient(getActivity())); mWebView.setWebChromeClient(new MyWebChromeClient(getActivity())); mWebView.getSettings().setDomStorageEnabled(true); mWebView.getSettings().setPluginState(WebSettings.PluginState.ON); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.addJavascriptInterface(this, "jsinterface"); // default go to video page mWebView.loadUrl("file://" + getActivity().getFilesDir().toString() + StorageUtils.finalfoldername.toString() + "video_list.html"); return rootView; }
How to add the custom view to implement the pull down refresh? Thanks