Laura Saraiva
Laura Saraiva

Reputation: 147

Progress Bar does not appear until recyclerView is displayed

I am developing an event and patrimony app for android. I have a problem. When I click on the category, the list of items appears. But while data is being fetched from the database, progressBar does not appear.

Can someone help me?

My code:

Item_Layout_Activity.java

    public class Item_Layout_Activity extends Fragment {

    RecyclerView recyclerView;
    String hash,email,tipo_login="";
    ArrayList<Hotel> array_hotel;
    ArrayList<Patrimony> array,array_temp;
    double latitudeUser,longitudeUser;
    String tipo_categoria;
    ProgressBar progressBar_app;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_list_others, container, false);
        recyclerView = (RecyclerView) root.findViewById(R.id.rv);

        progressBar_app=root.findViewById(R.id.progressBar_app);

        progressBar_app.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        RecyclerView.LayoutManager rvLiLayoutManager = layoutManager;
        recyclerView.setLayoutManager(rvLiLayoutManager);

        tipo_categoria= DrawerActivity.categoria_tab_outros;

        array=new ArrayList<>();
        array_temp=new ArrayList<>();

        if (tipo_categoria != null) {
           
            switch (tipo_categoria) {

                case "Hotel":

                    // will show the list of hotels
                    if(Controller.array_items_hotels!=null ) {

                            if (array_hotel != null)
                                array_hotel.clear();

                        if(array_hotel==null)
                            array_hotel=new ArrayList<>();

                            for (Hotel h: Controller.array_items_hotels) {
                                        if (h.getLocalidade().equals(Controller.localidade)) {
                                            array_hotel.add(h);
                                        }
                                    }
                            }

                            recyclerView.setVisibility(View.VISIBLE);
                            Adapter_Item adapter_item = new Adapter_Item(getContext(), array_hotel, "hotel");
                            recyclerView.setAdapter(adapter_item);

                    }// if the array does not yet exist then fetch the hotels a database
                    else
                    {

                        boolean res=Controller.VerificarConetividade();
                        if(res)
                        {
                            // search list of hotels at BD
                            buscarHoteisBD();
                        } else
                            Toast.makeText(getContext(), R.string.alerta_conexao_rede,Toast.LENGTH_LONG).show();

                    }

                    break;
        }
    }


// get list of hotels from the database
    private  void buscarHoteisBD(){

        Retrofit.Builder builder = new Retrofit.Builder().baseUrl(WebAPI.BASE_URL).addConverterFactory(GsonConverterFactory.create());
        Retrofit retrofit = builder.build();
        WebAPI webApi = retrofit.create(WebAPI.class);
        
        Call<ArrayList<Hotel>> call = webApi.get_Hotels(email_encrypt,tipo_login,hash,linguagem);

        call.enqueue(new Callback<ArrayList<Hotel>>() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onResponse(Call<ArrayList<Hotel>> call, Response<ArrayList<Hotel>> response) {

                array_hotel = response.body();

                if (array_hotel == null) {
                    Toast.makeText(getContext(), R.string.alerta_falha_BD, Toast.LENGTH_SHORT).show();
                } else {
                    Controller.array_items_hotels.addAll(array_hotel);
                    array_hotel.clear();
                    for (Hotel p:Controller.array_items_hotels) {

                                    array_hotel.add(p);
                            }
                    }
         }
                recyclerView.setVisibility(View.VISIBLE);
                Adapter_Item adapter_item = new Adapter_Item(getContext() ,array_hotel,"hotel");
                recyclerView.setAdapter(adapter_item);
            }

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onFailure(Call<ArrayList<Hotel>> call, Throwable t) {
                Toast.makeText(getContext(), R.string.alerta_falha_BD, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

fragment_list_others.xml

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fotolayout">

     <androidx.recyclerview.widget.RecyclerView
         android:id="@+id/rv"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

    <ProgressBar
        android:id="@+id/progressBar_app"
        style="?android:attr/progressBarStyle"
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:layout_gravity="center"
        android:indeterminateDrawable="@drawable/circularprogress"
        android:visibility="gone" />
</FrameLayout>

[![list items][1]][1] [1]: https://i.sstatic.net/DRJEU.jpg

Upvotes: 0

Views: 352

Answers (1)

van_izem
van_izem

Reputation: 66

Method onCreateView should return a view which should contain all the items and a progress bar you are trying to make visible. But since you adding all the items synchronously, in the main (UI) thread, method will return a view (together with progress bar) only when all items added.

I would try to make the following:

  • remove the line "progressBar_app.setVisibility(View.VISIBLE);"
  • add line "progressBar_app.setVisibility(View.GONE);" before returning a view from method onCreateView
  • make progress bar visible by default (android:visibility="visible") -it will become hidden when all items added.

Upvotes: 1

Related Questions