Using the “animated circle” in an ImageView while loading stuff

后端 未结 6 715
轻奢々
轻奢々 2020-11-29 14:27

I am currently using in my application a listview that need maybe one second to be displayed.

What I currently do is using the @id/android:empty property of the list

6条回答
  •  春和景丽
    2020-11-29 15:09

    If you would like to not inflate another view just to indicate progress then do the following:

    1. Create ProgressBar in the same XML layout of the list view.
    2. Make it centered
    3. Give it an id
    4. Attach it to your listview instance variable by calling setEmptyView

    Android will take care the progress bar's visibility.

    For example, in activity_main.xml:

        
    
    
        
    
       
    
    
    

    And in MainActivity.java:

    package com.fcchyd.linkletandroid;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import retrofit2.Call;
    import retrofit2.Callback;
    import retrofit2.Response;
    import retrofit2.Retrofit;
    import retrofit2.converter.gson.GsonConverterFactory;
    
    public class MainActivity extends AppCompatActivity {
    
    final String debugLogHeader = "Linklet Debug Message";
    Call call;
    List arraylistLink;
    ListView linksListV;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        linksListV = (ListView) findViewById(R.id.list_view_xml);
        linksListV.setEmptyView(findViewById(R.id.loading_progress_xml));
        arraylistLink = new ArrayList<>();
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.links.linklet.ml")
                .addConverterFactory(GsonConverterFactory
                        .create())
                .build();
    
        HttpsInterface HttpsInterface = retrofit
                .create(HttpsInterface.class);
    
        call = HttpsInterface.httpGETpageNumber(1);
    
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                try {
                    arraylistLink = response.body().getLinks();
    
                    String[] simpletTitlesArray = new String[arraylistLink.size()];
                    for (int i = 0; i < simpletTitlesArray.length; i++) {
                        simpletTitlesArray[i] = arraylistLink.get(i).getTitle();
                    }
                    ArrayAdapter simpleAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, simpletTitlesArray);
                    linksListV.setAdapter(simpleAdapter);
                } catch (Exception e) {
                    Log.e("erro", "" + e);
                }
            }
    
            @Override
            public void onFailure(Call call, Throwable t) {
    
            }
        });
    
    
    }
    

    }

提交回复
热议问题