Two ways to refresh the View view – the difference between invalidate() and postInvalidate() and detailed use

Hits: 0

1. Preliminary basic knowledge reserve

Invalidate English meaning: refresh

On, the official documentation:

From the official documentation, we can know that the invalidate() and postInvalidate() methods are both used to refresh the View interface/View view .

1) Difference: invalidate() is used to refresh the View and must work in the UI thread . For example, when modifying the display of a view, call invalidate() to see the redrawn interface. The call to invalidate() pops the old view from the main UI thread queue. postInvalidate() can be called directly in the child thread , and the page can be refreshed directly.

2) Specific implementation: ① Use the invalidate() method to refresh the page, borrow the Handler class , and rewrite the handleMessage method to call invalidate() to refresh the interface; and send the interface update message through the sendMessage() method in the thread; ② Use postInvalidate It is relatively simple, no Handler is required, and postInvalidate can be called directly in the thread.

Interested readers can refer to the author’s ” Android Thread Blocking Processing and Optimization” which describes the specific mechanism of Handler to achieve inter-thread communication.

Second, the above code, specifically implement invalidate() and postInvalidate() to refresh the page

① invalidate() method refreshes the View view

class DownloadThread extends Thread{

    @Override

    public void run() {

        try{

        Thread.sleep(5000);

        Message msg = new Message();

        msg.what = 1;

       //Send the Message to the corresponding Handler

        uiHandler.sendMessage(msg); //Note that it is still in a branch thread at this time

        }catch (InterruptedException e){

            e.printStackTrace();

    }

};

//uiHandler is created in the main thread, so the main thread is automatically bound

private Handler uiHandler = new Handler(){

    Override

    public void handleMessage(Message msg) {

        switch (msg.what){
            case 1:
            mGameView.invalidate(); // refresh the interface 
            break ;

        }

    }

};

② postInvalidate() method refreshes the page

class DownloadThread extends Thread{

    @Override

    public void run() {

        try{

        Thread.sleep(5000);

        }catch (InterruptedException e){

            e.printStackTrace();
         mGameView.postInvalidate(); // refresh the interface   
    }        
};

Summary: In fact, both methods are relatively simple to use. Common application scenarios, such as mobile phone Taobao, after opening a baby details page, continue to pull down, the page will continue to load, what appears to be loaded is a picture, but it is actually a View control, you can click to jump, this kind of The scenario involves refreshing the View view.

You may also like...

Leave a Reply

Your email address will not be published.