2016-07-12 7 views
2

私はリサイクラビューを持っていますが、各ビューではOxyPlotを使ってデータを表示しようとしています。RecyclerViewのOxyPlot -MVVMCross Xamarin.Android

各カードのハードコードされた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); } 
    } 
} 
+0

を含めますあなたのViewModelのように見える? XMLバインディングをhttps://stackoverflow.com/questions/38316666/binding-oxyplot-via-mvvmcross-in-xamarin-android/38336559#38336559に使用するだけで済みますが、リストのモデルに基づいています。 – Plac3Hold3r

+0

私の質問を実際の例で更新しましたが、今はパフォーマンスの問題があります。私はそれぞれのプロットを各カードに入れてスクロールすると、それは遅いです。私はどこが間違っているのだろうか? – hotspring

答えて

4

私は仕事に質問からあなたの例を取得することができませんでした。しかし、あなたが試みることができるのは、Adapterの標準クラスオブジェクトとしてViewModelを再構築するのではなく、レイアウトにデータ(プロットポイント)のバインディングを使用することです。


実装例:

の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レイアウトテンプレート。この点の使用は、モデル全体(この場合はPlotModel)にバインドするようにMvxに指示するために使用されますが、空白のままにすることもできます(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; 
} 

アップデート - あなたの "SALL" LISを何スクリーンショット

enter image description here

+0

すべての努力のために多くのおかげで、さらにははっきりとした説明のために。 – hotspring

+1

問題ないです、うれしかったです。 – Plac3Hold3r

+0

@Placeholder、私はちょうどあなたの解決策をテストする機会を得ましたが、何らかの理由で何も表示されません。あなたはテストしましたか? – hotspring

関連する問題