RecyclerView 中的 OxyPlot -MVVMCross Xamarin.Android

OxyPlot in RecyclerView -MVVMCross Xamarin.Android

我有以下实现,其中有回收器视图,在每个视图中我都尝试使用 OxyPlot 显示数据。

我可以在每张卡片上看到硬编码的 Plotvalues,但是当我滚动时,它的响应有点慢并且应用程序会冻结一段时间。我想知道我做错了什么或者如何改善这个性能问题?

MainView.xml

<MvxRecyclerView
  android:id="@+id/myRecyclerView"
  android:layout_marginTop="10dp"
  android:scrollbars="vertical"
  android:divider="@null"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  local:MvxItemTemplate="@layout/mycardview" />

mycardview.xml

<RelativeLayout
    android:layout_width="200dp"
    android:layout_height="match_parent">
    <oxyplot.xamarin.android.PlotView
       android:id="@+id/Plot"
       android:layout_width="match_parent"
       android:layout_height="match_parent" /> 
</RelativeLayout>

MainView.cs

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ 
   var ignored = base.OnCreateView(inflater, container, savedInstanceState);
   var view = this.BindingInflate(Resource.Layout.MainView, null);
   HasOptionsMenu = true;
   var cardRecyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.myRecyclerView);
   if (cardRecyclerView != null)
   {
       cardRecyclerView.HasFixedSize = false;
       cardRecyclerView .Adapter = new MainViewRecyclerAdapter((IMvxAndroidBindingContext)BindingContext, Activity);
       var layoutManager = new LinearLayoutManager(Activity);
       cardRecyclerView.SetLayoutManager(layoutManager);
    }

  return view;
}

MainViewRecyclerAdapter.cs

public class MainViewRecyclerAdapter : MvxRecyclerAdapter
{

  private readonly FragmentActivity _activity;
  public MainViewRecyclerAdapter(IMvxAndroidBindingContext bindingContext, FragmentActivity activity)
        : base(bindingContext)
  {
      _activity = activity;
  }

  public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
  {
      base.OnBindViewHolder(holder, position);

      var view = holder.ItemView;
      var cardOptionsButton = view.FindViewById<PlotView>(Resource.Id.Plot);
      MainViewModel MyMainViewModel = new MainViewModel();
      cardOptionsButton.Model = MyMainViewModel.MyModel;
   }
}

MyMainViewModel.cs

public class MyViewModel : MvxViewModel
{
    public MyViewModel()
    {
        GeneratePlotPoints();
    }

    void GeneratePlotPoints()
    {
        var mo = new PlotModel();
        var s1 = new LineSeries()
        {
            Color = OxyColors.SkyBlue,
            MarkerType = MarkerType.Circle,
            MarkerSize = 6,
            MarkerStroke = OxyColors.White,
            MarkerFill = OxyColors.SkyBlue,
            MarkerStrokeThickness = 1.5
        };
        s1.Points.Add(new DataPoint(0, 10));
        s1.Points.Add(new DataPoint(10, 40));
        s1.Points.Add(new DataPoint(40, 20));
        s1.Points.Add(new DataPoint(60, 30));
        mo.Series.Add(s1);
        MyModel = mo;
    }

    PlotModel _myModel;
    public PlotModel MyModel
    {
        get { return _myModel; }
        set { SetProperty(ref _myModel, value); }
    }
}

我无法从问题中得到你的例子。但是,您可以尝试将数据(绘图点)绑定到布局,而不是将 ViewModel 重新构造为 Adapter 中的标准 class 对象。


实现示例:

ViewModel

为简单起见,我刚刚创建了一个简单的 ObservableCollection,其中包含重复几次的相同绘图点组(图形形状)。 更新: 作为 PlotModel can only be used once in an PlotView,您必须确保 PlotModel 始终是一个新实例。

public class MyViewModel : BaseViewModel
{
    PlotModel GeneratePlotPoints()
    {
        var mo = new PlotModel();
        var s1 = new LineSeries()
        {
            Color = OxyColors.SkyBlue,
            MarkerType = MarkerType.Circle,
            MarkerSize = 6,
            MarkerStroke = OxyColors.White,
            MarkerFill = OxyColors.SkyBlue,
            MarkerStrokeThickness = 1.5
        };

        s1.Points.Add(new DataPoint(0, 10));
        s1.Points.Add(new DataPoint(10, 40));
        s1.Points.Add(new DataPoint(40, 20));
        s1.Points.Add(new DataPoint(60, 30));
        mo.Series.Add(s1);

        return mo;
    }

    List<PlotModel> GenerateGraph()
    {
        var graphPlots = new List<PlotModel>();
        int counter = 0;

        while (counter < 10)
        {
            graphPlots.Add(GeneratePlotPoints());
            counter++;
        }

        return graphPlots;
    }

    public List<PlotModel> GraphPlots => GenerateGraph();
}

布局

RecyclerView 的主要布局。

<MvxRecyclerView
    android:id="@+id/myRecyclerView"
    android:layout_marginTop="10dp"
    android:scrollbars="vertical"
    android:divider="@null"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    local:MvxBind="ItemsSource GraphPlots"
    local:MvxItemTemplate="@layout/mycardview" />

mycardview 布局模板。请注意,点的使用用于告诉 Mvx 绑定到整个模型(在本例中为 PlotModel),但它也可以留空(Mvx doc link)。

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <oxyplot.xamarin.android.PlotView
     android:id="@+id/Plot"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     local:MvxBind="Model ."/>
</RelativeLayout>

查看

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ 
   var ignored = base.OnCreateView(inflater, container, savedInstanceState);
   var view = this.BindingInflate(Resource.Layout.MainView, null);
   HasOptionsMenu = true;
   var cardRecyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.myRecyclerView);
   if (cardRecyclerView != null)
   {
       cardRecyclerView.HasFixedSize = false;
       var layoutManager = new LinearLayoutManager(Activity);
       cardRecyclerView.SetLayoutManager(layoutManager);
    }

    return view;
}

更新 - 包括截图