ProgressBar没有显示 - 等待

时间:2017-04-27 19:33:46

标签: c# android xamarin.android

当我从数据库加载数据时,我无法显示进度微调器。

这是axml页面上的进度条:

<ProgressBar
      android:id="@+id/HomeProgressBar"
      android:visibility="invisible"
      android:layout_centerInParent="true"
      android:theme="@style/CircularProgress"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      style="@style/Widget.AppCompat.ProgressBar"/>

在活动页面上创建进度条对象:

public class HomeActivity : Activity
    {
      private ProgressBar progressBar;

使用进度条的方法:

private async System.Threading.Tasks.Task LoadPostData()
    {
        mListView = FindViewById<ListView>(Resource.Id.lstHomeItems);
        mItems = new List<Post>();

        progressBar.Visibility = ViewStates.Visible;
        mListView.Visibility = ViewStates.Invisible;

        var firebase = new FirebaseClient(FirebaseURL);
        var posts = await firebase.Child("posts").OnceAsync<Post>();

        mItems.Clear();
        adapter = null;
        foreach (var item in posts)
        {
            Post post = new Post();
            post.ActivityID = item.Key;
            post.OwnerID = item.Object.OwnerID;
            post.ActivityTitle = item.Object.ActivityTitle;
            post.ActivityDetails = item.Object.ActivityDetails;
            post.ActivityDate = item.Object.ActivityDate;
            post.ActivityTime = item.Object.ActivityTime;
            post.ActivityAddress = item.Object.ActivityAddress;
            post.ActivityTown = item.Object.ActivityTown;
            post.ActivitySport = item.Object.ActivitySport;
            post.ActivityEmail = item.Object.ActivityEmail;
            post.ActivityPhone = item.Object.ActivityPhone;

            // Remove Old events
            TimeSpan ts = DateTime.Now - post.ActivityDate;

            // Remove event if it is old
            if (ts.TotalDays > 1)
            {
                // Remove old posts from the database
                await firebase.Child("posts").Child(item.Key).DeleteAsync();
            }
            else
            {
                mItems.Add(post);
            }
        }

        adapter = new HomeListViewAdapter(this, mItems);
        adapter.NotifyDataSetChanged();
        mListView.Adapter = adapter;

        // Hide ProgressBar when data is loaded
        progressBar.Visibility = ViewStates.Invisible;
        mListView.Visibility = ViewStates.Visible;
    }

这就是我调用LoadPostData方法并从OnCreate填充进度条的方法:

// Spinning Progress Bar
            progressBar = FindViewById<ProgressBar>(Resource.Id.HomeProgressBar);

await LoadPostData();

2 个答案:

答案 0 :(得分:0)

所有用户界面管理必须在主线程中完成。您应该使ProgressBar出现在主线程中,然后创建加载数据的任务。

或者使用AsyncTask并在preExecute中显示ProgressBar并在postExecute中隐藏它,两个方法都将在主线程上调用。

答案 1 :(得分:0)

您只能更新主线程中的UI。否则它将无法工作。试试运行主线程:

YourActivity.runOnUiThread(new Runnable()      
        {      
            public void run()      
            {      
                yourProgressBar.show();   
            }      

        });

不建议这样做。更好的方法是使用asyncTask并将您的UI更新放在onPostExecute();