Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Progressbar until RecyclerView is filled

Posted on November 14, 2022

Solution 1 :

From what I understand, you need to handle 2 states:

  1. LOADING – When the search button is clicked and you are getting your data from Api Or Db.
  2. LOADED – When the data is fetched, and set in the adapter of the recyclerview.
   // In loading state,clear data, hide recyclerview and show progressbar
   public void showLoadingState() {
      pb_Search.setVisibility( View.VISIBLE );
      rv_items.setVisbility(View.GONE);
      // Clear your adapter here, otherwise old content will
      // show when you do 2 searches in a row.
   }

   // In loaded state, show recyclerview and hide progressbar
   public void showLoadedState() {
      pb_Search.setVisibility( View.GONE );
      rv_items.setVisbility(View.VISIBLE);
   }

Problem :

I created an EditText that is used in order to search.

Once the user click the search button I show a ProgressBar until the RecyclerView is loaded by doing this:

et_Search.setOnEditorActionListener( (v, actionId, event) -> {
    if (actionId == EditorInfo.IME_ACTION_SEARCH) {

        InputMethodManager imm =(InputMethodManager) getSystemService( Context.INPUT_METHOD_SERVICE);
        View view = this.getCurrentFocus();
        imm.hideSoftInputFromWindow( view.getWindowToken(), 0);

        pb_Search.setVisibility( View.VISIBLE );

        query = et_Search.getText().toString();

        fetchAddItems(query);

        return true;
    }
    return false;
} );

Where:

private void fetchAddItems(String query) {

    B_client = new ItemClient();
    B_client.getItems( query, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            List<Item> Items;
            try {
                JSONArray items;
                if (response != null) {
                    items = response.getJSONArray( "items" );
                    Items = Item.fromJson( items );

                    AddItemsAdapter = new AddItemAdapter(Items, listenerAdd);
                    rv_Items.setAdapter(AddItemsAdapter);

                    rv_Items.getViewTreeObserver()
                        .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                pb_Search.setVisibility( View.GONE );
                            }
                        });

                }
            } catch (JSONException ignored) {

            }
        }
    } );

}

Everything works ok.

The problem is in the case where the user performs two searches in a row.

In this case, the RecyclerView is already filled from the first search, and therefore the ProgressBar won’t show.

Is there a way to do what I want in both cases when the RecyclerView is filled/updated?

READ  [ANSWERED] Android back arrow has no visual feedback on click
Powered by Inline Related Posts

Thank you

SOLUTION:

What solved my problem is removing the listener and using this:

rv_Items.getViewTreeObserver()
    .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            rv_Items.setVisibility( View.VISIBLE );
            pb_Search.setVisibility( View.GONE );
            rv_Items.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });

Comments

Comment posted by Ben

Im not sure if this is the problem since I do reset my list from my understanding. I added the code.

Comment posted by Aman Arora

I only see you setting new data in adapter, but if in layout hierarchy your recyclerview is above the progressbar, then on search click progress won’t show if you don’t hide the recyclerview.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress