7

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 call refreshToDo() , when I release it , it go back to top and hide) , but how to implement that? thanks

layout: (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

user782104
  • 13,233
  • 55
  • 172
  • 312
  • There are libraries for this, such as http://www.androidviews.net/2013/08/actionbar-pull-to-refresh/ and http://www.androidviews.net/2012/10/pull-to-refresh/ – CommonsWare Jan 10 '14 at 17:52

5 Answers5

11

Now there is an official widget SwipeRefreshLayout in the Android support library. It is doing exactly what you are looking for. The documentation is here:

https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

Here is a good tutorial I found:

http://antonioleiva.com/swiperefreshlayout/

9

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);
        }
    }
});
ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73
Choletski
  • 7,074
  • 6
  • 43
  • 64
3

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.

Or Bar
  • 1,566
  • 11
  • 12
1

You can use SwipeRefreshLayout available in android support library.

A complete sample code example with tutorial is avaialable at this link

0

Try this: LisView Custom XListView-Android

Quang Doan
  • 632
  • 4
  • 12