Xamarin:从async func更新listview项

时间:2017-09-28 13:11:27

标签: android listview xamarin android-asynctask notifydatasetchanged

我正在使用Xamarin.Media.Plugin使用异步函数CrossMedia.Current.TakePhotoAsync从相机拍照。如果照片已经制作,我需要更新listview项目。此代码工作正常,但在慢速设备上,更新项目太慢,如果在这段时间内,用户更改了listview中的项目,将更新新项目,而不是普及。我怎么解决这个问题?

private async void BtnPhoto_ClickAsync(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            MediaFile mediaFile = await CrossMedia.Current.TakePhotoAsync(opt);

            if (mediaFile != null)
            {
                Shared.CurrentQuest.Photo[listViewHydromer.CheckedItemPosition] = mediaFile.Path;
                adapter.SetCustomElement("labelPhoto", "Exist");
                adapter.NotifyDataSetChanged();
                mediaFile.Dispose();
            }
        }

CustomAdapter代码:

public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View row = convertView;

        row = row ?? LayoutInflater
            .From(context)
            .Inflate(Resource.Drawable.hydromerListViewContentLine, null, false);

        //События для контролов
        labelIsPhotoAvailable.Click += (s, e) => ShowPhotoDetails(position);

        //Присваиваем полям значения
        labelId.Text = $"Водомер: {hydromers.ElementAt(position).Id}";
        labelDate.Text = $"Последняя поверка: {hydromers.ElementAt(position).LastDate}";
        labelLastIndication.Text = "Последние показания: ";
        labelCurrentIndication.Text = "Текущие показания: ";

        return row;
    }

SetCustomElemetns方法:

public void SetCustomElement(string element, string value)
    {
        if(element == "labelCurrentIndication")
          labelCurrentIndication.Text = $"Текущие показания: { value}";

        if (element == "labelIsPhotoAvailable")
          labelIsPhotoAvailable.Text = "Фото готово";

    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1">
        <TextView
            android:text="Водомер 1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:paddingLeft="4sp"
            android:layout_toRightOf="@id/linearLayout1"
            android:id="@+id/hydromerListView_LineId" />
        <TextView
            android:text="Последняя поверка:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:paddingLeft="4sp"
            android:layout_toRightOf="@id/linearLayout1"
            android:id="@+id/hydromerListView_LineLastDate" />
        <TextView
            android:text="Последние показания:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:paddingLeft="4sp"
            android:layout_toRightOf="@id/linearLayout1"
            android:id="@+id/hydromerListView_LineLastIndication" />
        <TextView
            android:text="Текущие показания:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:paddingLeft="4sp"
            android:layout_toRightOf="@id/linearLayout1"
            android:id="@+id/hydromerListView_LineCurrentIndication" />
    </LinearLayout>
    <TextView
        android:text="Нет фото"
        android:layout_width="wrap_content"
        android:layout_height="65sp"
        android:textSize="16sp"
        android:paddingRight="25sp"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:id="@+id/hydromerListView_IsPhotoAvailable" />
</RelativeLayout>

0 个答案:

没有答案