Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在异步加载期间ProgressBar未旋转_Android_Xamarin - Fatal编程技术网

Android 在异步加载期间ProgressBar未旋转

Android 在异步加载期间ProgressBar未旋转,android,xamarin,Android,Xamarin,当我打开一个片段时,我尝试显示一个进度条,填充一个回收视图,将通知更改更新为回收视图,然后隐藏进度条 现在ProgressBar没有旋转。我认为这与在UI线程上执行SetRecycleServiceItems有关,但如果我不这样做,我会得到一个错误,即UI只能在UI线程中更新 片段: public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

当我打开一个
片段
时,我尝试显示一个
进度条
,填充一个
回收视图
,将
通知更改
更新为
回收视图
,然后隐藏
进度条

现在ProgressBar没有旋转。我认为这与在UI线程上执行SetRecycleServiceItems有关,但如果我不这样做,我会得到一个错误,即UI只能在UI线程中更新

片段:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            /* Initializing elements code removed */

            SetRecyclerView();

            return view;
        }

    public async override void OnStart()
            {
                base.OnStart();

                ShowProgressBar();
                await LoadAsync();
                HideProgressBar();
            }

            private async Task LoadAsync()
            {
                await Task.Run(() => {
                    SetRecyclerViewItems();
                });

            }

            private void SetRecyclerView()
            {
                mLayoutManager = new LinearLayoutManager(mRecyclerView.Context);
                mRecyclerView.SetLayoutManager(mLayoutManager);
                mAdapter = new TransactionsRecyclerAdapter(this.Activity, mTransactions, dateFormat);
                mAdapter.ItemClick += MAdapter_ItemClick;
                mRecyclerView.SetAdapter(mAdapter);
            }

            private void SetRecyclerViewItems(List<PaymentListItemViewModel> transactions = null)
            {
                Activity.RunOnUiThread(() => {

                    if (transactions == null)
                        transactions = GetTransactions();
                    mAdapter.NotifyChange(transactions);
                    SetTotal();
                });
            }
在OnCreateView中:

mLayoutProgressBar = view.FindViewById<RelativeLayout>(Resource.Id.transactionsProgressBar);

通过使用
Task.Run()
将加载放在一个单独的线程上,但在那里您将封送回UI线程以更新适配器(
Activity.RunOnUiThread()
),这将阻止UI线程,并且微调器停止运行


根据您的
SetTotal()
方法正在执行的操作,您应该只调用
mAdapter.NotifyChange(事务)在UI线程上执行。

显然,这是因为setRecycleServiceItems中的RunOnUiThread只是Handler.post的快捷方式,它会立即返回,并使所有这些都异步,Wait and task Useless GetTransactions和SetTotal是什么?它们需要多长时间?这是我的想法,但如前所述,如果我不在UI线程中运行它,会抛出一个异常,说“视图只能在UI线程中更改”。如何分别运行两个UI项(ProgressBar和SetRecycleServiceItems)?@Luke创建适配器时,会用一个空列表填充适配器,然后使用GetTransactions获取要在ReycleView中使用的数据。处理大约需要1秒。SetTotal目前是一个空方法。显然,您应该只在task中运行GetTransactions并等待其结果谢谢您的时间。我在BindViewHolder中进行了大量计算(这阻止了UI线程),在您的帮助下,我现在异步加载所有数据,只将实际数据发送到RecyclerView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_background">
    <RelativeLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:id="@+id/transactionsProgressBar">
        <ProgressBar
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            style="@android:style/Widget.ProgressBar.Large" />
    </RelativeLayout>
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout4"
        android:layout_weight="1">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewTransactions"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:scrollbars="vertical" />
    </LinearLayout>

</LinearLayout>
mLayoutProgressBar = view.FindViewById<RelativeLayout>(Resource.Id.transactionsProgressBar);
private void ShowProgressBar()
                {
                    mLayoutProgressBar.Visibility = ViewStates.Visible;
                }

                private void HideProgressBar()
                {
                    mLayoutProgressBar.Visibility = ViewStates.Gone;
                }