Mubarak Awal
Mubarak Awal

Reputation: 489

How do i show the progress bar while recyclerview is loading and hide it when done?

i have a recyclerview which displays data requested by volley. I'm using the handler().postDelayed to show the progress bar for 4 seconds but it's not consistent with my the recyclerview because sometimes it takes more than 4 seconds to get data from api and display it in the recyclerview and sometimes it takes less tham that. But i want the progress bar to run as long as the recyclerview isn't visible yet.

This is my code

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            stopProgressbar();
        }
    },4000);

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            loadData();
        }
    });

Upvotes: 0

Views: 2652

Answers (2)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

Call stopProgressbar() from onResponse() and onErrorResponse() method.

Here is an example:

public void loadData()
{
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
            url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());

                    // Parse JSON data and add to list
                    ...........
                    ...........................

                    // Hide progressbar
                    stopProgressbar();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());

                    // Hide progressbar
                    stopProgressbar();                    
                }
            });

            ..................
            ..........................
 }

Upvotes: 2

Shabbir Dhangot
Shabbir Dhangot

Reputation: 9121

No need of separate handler to handle progressbar.

You can stop once you get response from the volly. You should call stopProgressbar(); method from there.

If and only If you facing some main thread issue write it under runOnUIThread() method.

Upvotes: 1

Related Questions