how to show progress bar(circle) in an activity having a listview before loading the listview with data

后端 未结 12 2483
忘掉有多难
忘掉有多难 2020-11-28 00:18

I have a ListView in my second activity.OnItemClick of it I called a webservice and trying to fetch data. And after that I am moving to third activ

12条回答
  •  天涯浪人
    2020-11-28 01:15

    There are several methods of showing a progress bar (circle) while loading an activity. In your case, one with a ListView in it.

    IN ACTIONBAR

    If you are using an ActionBar, you can call the ProgressBar like this (this could go in your onCreate()

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
    setProgressBarIndeterminateVisibility(true);
    

    And after you are done displaying the list, to hide it.

    setProgressBarIndeterminateVisibility(false);
    

    IN THE LAYOUT (The XML)

    
    
        
    
            
            
        
    
        
        
    
    

    And in your activity (Java) I use an AsyncTask to fetch data for my lists. SO, in the AsyncTask's onPreExecute() I use something like this:

    // CAST THE LINEARLAYOUT HOLDING THE MAIN PROGRESS (SPINNER)
    LinearLayout linlaHeaderProgress = (LinearLayout) findViewById(R.id.linlaHeaderProgress);
    
    @Override
    protected void onPreExecute() {    
        // SHOW THE SPINNER WHILE LOADING FEEDS
        linlaHeaderProgress.setVisibility(View.VISIBLE);
    }
    

    and in the onPostExecute(), after setting the adapter to the ListView:

    @Override
    protected void onPostExecute(Void result) {     
        // SET THE ADAPTER TO THE LISTVIEW
        lv.setAdapter(adapter);
    
        // CHANGE THE LOADINGMORE STATUS TO PERMIT FETCHING MORE DATA
        loadingMore = false;
    
        // HIDE THE SPINNER AFTER LOADING FEEDS
        linlaHeaderProgress.setVisibility(View.GONE);
    }
    

    EDIT: This is how it looks in my app while loading one of several ListViews

    enter image description here

提交回复
热议问题