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

后端 未结 6 714
轻奢々
轻奢々 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:21

    This is generally referred to as an Indeterminate Progress Bar or Indeterminate Progress Dialog.

    Combine this with a Thread and a Handler to get exactly what you want. There are a number of examples on how to do this via Google or right here on SO. I would highly recommend spending the time to learn how to use this combination of classes to perform a task like this. It is incredibly useful across many types of applications and will give you a great insight into how Threads and Handlers can work together.

    I'll get you started on how this works:

    The loading event starts the dialog:

    //maybe in onCreate
    showDialog(MY_LOADING_DIALOG);
    fooThread = new FooThread(handler);
    fooThread.start();
    

    Now the thread does the work:

    private class FooThread extends Thread {
        Handler mHandler;
    
        FooThread(Handler h) {
            mHandler = h;
        }
    
        public void run() { 
            //Do all my work here....you might need a loop for this
    
            Message msg = mHandler.obtainMessage();
            Bundle b = new Bundle();                
            b.putInt("state", 1);   
            msg.setData(b);
            mHandler.sendMessage(msg);
        }
    }
    

    Finally get the state back from the thread when it is complete:

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            int state = msg.getData().getInt("state");
            if (state == 1){
                dismissDialog(MY_LOADING_DIALOG);
                removeDialog(MY_LOADING_DIALOG);
            }
        }
    };
    

提交回复
热议问题