0

So here is the method I found in most tutorials of SwipeRefreshLayout but it seems complitely stupid to me.

What it does : it does the refresh animation for 2000 ms before actually doStuff().

What I (obviously!!) want to do : refresh animation while it does doStuff() then stop. No timer needed ! Am I doing something wrong ? Internet tells me no...

            view.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    swipeEmptyView.setRefreshing(true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            doStuff();
                            swipeEmptyView.setRefreshing(false);
                        }
                    }, 2000);
                }
            });
Waroulolz
  • 297
  • 9
  • 23
  • How would you implement your "doStuff()" method? – Nguyễn Hoài Nam Apr 01 '15 at 23:54
  • public void doStuff(){ System.out.println("Test");} or I don't know anything that takes a bit time. I want it to keep refreshing while doing stuff. – Waroulolz Apr 01 '15 at 23:57
  • 1
    FYI, most tutorial about SRL uses postDelayed to show you how it work, so the point is not the postDelayed part. Assume that you want to make a http request to refresh your data, you "obviously" have to add it into an AsyncTask. So consider to use its onPreExecute method to start loading animation, and stop the animation in onPostExecute. I can help you an implement in code if you couldn't – Nguyễn Hoài Nam Apr 01 '15 at 23:58
  • In this case, your loading process will end immediately, there is no point of loadig animation here mate ;). – Nguyễn Hoài Nam Apr 01 '15 at 23:59
  • You answer with the http request example seems to do what I want. I'll look for the onPreExecute methods u mentionned ! Maybe I'll need help ! Thanks – Waroulolz Apr 02 '15 at 00:01
  • Do you have some interesting links to propose ? I don't find anything relevant. Also could you explain why http request has to be AsyncTask and how to implement it ? (I think I understand why but it is to be shure). – Waroulolz Apr 02 '15 at 00:26

1 Answers1

1

The response for your comment should be an answer.

  1. The point of http request is to run a hard work in background, that is what AsyncTask is designed for. Otherwise, you will face the Exception of running network request on Mainthread, please Google it to figure out more.

  2. About the SwipeRefreshLayout, I think it will show the loading animation automatically UNTIL you call the mSwipeLayout.setRefreshing(false). So there are 2 kinds of implement here:

2.1 - Use AsyncTask for http request, use your own loading animation (with or without SwipeRefreshLayout):

public class MyAsyncTask<DummyStuff> extends AsyncTask<Void, Void, Void> {

    public void onPreExecute() {
        // start your animation here
        // NOTE: if you use SwipeRefreshLayout, it will automatically show animation when you swipe your layout down, so please consider your UX to do what you want.
    }

    public void onDoinBackground(Void... input) {
        // do something
        // http request or something
        return null;    // return what ever you get
    }

    public void onPostExecute(Void... result) {
        // stop your animation here
        // in case you use SwipeRefreshLayout, call mSRL.setRefreshing(false) here too.
    }
}

2.2 User above Task with your SwipeRefreshLayout:

I will show only the important parts:

mSRL.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        (new MyAsyncTask()).execute();
    }
});

Above is just the idea about what should be done where, please try it yourself.

I will update this answer later, with a real sample. But I hope you get the idea here.

Nguyễn Hoài Nam
  • 1,130
  • 1
  • 9
  • 20
  • @Waroulolz please consider this sample: https://github.com/googlesamples/android-SwipeRefreshListFragment – Nguyễn Hoài Nam Apr 02 '15 at 02:17
  • Thank you, I managed to make it work. Actually I also know what I want to do while refreshing. I would like to fetch some data (mostly text) from the internet but not necesseraly à website! I would like to have a server that allows clients to fetch some text data. What kind of server fits the best to my goals? Http or maybe simply tcp? I don't know much about http so I don't know if it matches my goals and/or if it handles well a kind of text "database". – Waroulolz Apr 02 '15 at 23:57
  • You may want to try Google App Engine at first. Here is a sample: https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/HelloEndpoints For more information, please create a new question ;). – Nguyễn Hoài Nam Apr 03 '15 at 05:34