0

我有一个自定义对话框,它是由下面的代码创建的:

public DialogFragment CreateNewPostedMessageDialog(CardSwipeData data, 
    List<MessagesMap> messagesMap, 
    string fbProfileimageAsByteString, 
    Context context) {
            DialogFragment newFragment = 
                new NewPostedMessageDialogFragment(data, messagesMap,
                                                   fbProfileimageAsByteString, 
                                                   context);
            return newFragment;
        }

它是从我的 Activity 的 OnResume RunOnUiThread 调用的:

ThreadPool.QueueUserWorkItem(state => {
    // Processing stuff here               

    RunOnUiThread(() => {
        DialogFragment dialog = CreateNewPostedMessageDialog(cardSwipeData,
           messagesMap, bitmapByteString, this);

        dialog.Show(FragmentManager, "PostedMessage");

        // ListAdapter gets updated here

        Thread.Sleep(3000);

        dialog.Dismiss();
    });
});

我想在 3 秒后关闭我的对话框,但发生的情况是我的对话框从未出现,但我的列表在 3 秒后仍会更新。我在睡眠方面做错了什么?

4

2 回答 2

2

Since runOnUiThread runs on the UI thread

Thread.Sleep(3000);

blocks the UI thread for three seconds, making the UI unresponsive . If you want to dismiss the Dialog after three seconds you can use the postDelayed() from the Handler class:

Declare an Handler handler = new Handler();

then, inside the runOnUiThread change the code you post with:

   {

      final DialogFragment dialog = CreateNewPostedMessageDialog(cardSwipeData,
       messagesMap, bitmapByteString, this);

     dialog.Show(FragmentManager, "PostedMessage");

    // ListAdapter gets updated here

     handler.postDelayed( new Runnable() {

          @Override
          public void run() {
             dialog.Dismiss();
          }
     }, 3000) ;

});

check for typo

于 2013-01-16T15:32:39.370 回答
1

您做错了什么是您正在休眠 UI 线程,而不是您在TreadPool. 试试这个:

ThreadPool.QueueUserWorkItem(state => {
    // Processing stuff here               

    DialogFragment dialog;

    RunOnUiThread(() => {
        dialog = CreateNewPostedMessageDialog(cardSwipeData,
           messagesMap, bitmapByteString, this);

        dialog.Show(FragmentManager, "PostedMessage");
    });

    // ListAdapter gets updated here
    Thread.Sleep(3000);

    RunOnUiThread(() => dialog.Dismiss()); 
});
于 2013-01-16T15:35:14.433 回答