Android - using runOnUiThread to do UI changes from a thread

后端 未结 5 1214
走了就别回头了
走了就别回头了 2020-12-06 04:18

I am using a custom title view and would like to show/hide a progressbar in the title view when a thread is working.

This is my title view\'s XML

<         


        
相关标签:
5条回答
  • 2020-12-06 04:53

    Few Things to try:

    1) (This probably isn't it) Make sure "titleProgress" is volatile.

    2) Try throwing a few postInvalidate() or titleProgress.postInvalidate() in there to trigger a redraw.

    3) Have you sacraficed a x486 machine on an alter resembling a giant green robot? (just kidding)

    Let me know if those first two (and if you're really desperate, the third) get you anywhere.

    0 讨论(0)
  • 2020-12-06 05:07

    use Communication with Handlers ,

    1. execute your thread
    2. into run() : you should send a message to the Handler which will update the progressBar (Horizontal of course ) : handler.sendMessage(handler.obtainMessage());
    3. in your Activity , you should override the method handleMessage(Message msg ) : like this

      handler = new Handler(){
        @override
        public void handleMessage(Message msg ) 
        {
          //here you write the code which will update your progressBar 
        }
      };
      
    0 讨论(0)
  • 2020-12-06 05:11

    Is your layout correct? Did you try to get this working with a vertical layout first? Just to see if the progress bar is visible when you start your activity?

    0 讨论(0)
  • 2020-12-06 05:12

    What you should use in this case is AsyncTask, see http://developer.android.com/reference/android/os/AsyncTask.html

    If you're not targeting a pre-1.5 version of Android, you will have to use a different class called UserTask. See http://android-developers.blogspot.com/2009/05/painless-threading.html

    0 讨论(0)
  • 2020-12-06 05:13

    Try changing the style of the ProgressBar to horizonal:

    style="?android:attr/progressBarStyleHorizontal"
    

    The original

    style="?android:attr/progressBarStyleSmall"
    

    gives a circular "progressbar" which can't be fully seen in the titlebar.

    0 讨论(0)
提交回复
热议问题