简体   繁体   中英

how to implement pull-down to refresh in java

I am going to make a pull down refresh option for my webview , i got the xml code and its working, but i don't know how to make it work properly by using java, i am using app drawer activity and this is my first fragment (home_fragment in code)

here is the xml code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.fb.jaisonjoseph.facebookbasic.Home_Fragment">

    <!-- TODO: Update blank fragment layout -->

    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/webView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true" />

    </android.support.v4.widget.SwipeRefreshLayout>



    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:animationResolution="@integer/abc_max_action_buttons"
        android:clickable="false"
        android:theme="@style/Base.Theme.AppCompat"
        android:indeterminate="false"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

here is my java code in the Home_Fragment.java

package com.fb.jaisonjoseph.facebookbasic;


import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.view.KeyEvent;

/**
* A simple {@link Fragment} subclass.
*/
public class Home_Fragment extends Fragment {
    public ProgressBar bar;
    SwipeRefreshLayout mySwipeRefreshLayout;
    public WebView mwebView;

    public Home_Fragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_home_, null);
        final WebView view=(WebView) rootView.findViewById(R.id.webView);
        view.loadUrl("https://mbasic.facebook.com");
        view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new MyWebViewClient());
        view.getSettings().setBuiltInZoomControls(true);
        view.getSettings().setDisplayZoomControls(false);
        bar=(ProgressBar)rootView.findViewById(R.id.progressBar);
        return rootView;
    }

    private class MyWebViewClient extends WebViewClient {


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }

        @Override
        public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
            bar.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            bar.setVisibility(View.GONE);
            super.onPageFinished(view, url);
        }
    }
}

First declare swiperefresh in .Java file.

        SwipeRefreshLayout swiperefresh=(SwipeRefreshLayout) findViewById(R.id.swiperefresh); 

then you need to use setOnRefreshListener, so whenever the layout is swiped down, setOnRefreshListener will be called

        swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                     public void onRefresh() {
                     // code to refresh 

                    swiperefresh.setRefreshing(false);   //code to stop refresh animation

                     }
                  });

Disable vertical scrolling in WebView , set its 'height' attribute to 'wrap_content' . By doing so, WebView will take entire hight of web page. Then put WebView to ScrollView and it, in turn, into SwipeRefreshLayout .

Add this into your onCreateView() method ,

mySwipeRefreshLayout = (SwipeRefreshLayout)rootView.findViewById(R.id.swiperefresh);

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {  
    @Override
    public void onRefresh() {
        // logic to refresh then add the following line to stop refresh animation 

         mSwipeRefreshLayout.setRefreshing(false); 

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM