2017-05-15 155 views
1

我找到了android data binding with a custom view解釋,但這對我不起作用,因爲我不太瞭解這一點,而且我有一點不同的情況。使用MVVM實現自定義視圖

我的想法: 我需要畫布,所以我可以畫一些東西。 我做了一個擴展View類的類(CustomView)。在CustomView類中,我製作了負責繪製的服務實例,並且在重寫的onDraw方法中,我將canvas傳遞給了服務類,以便應用程序可以繪製。

問題: 在活動我都用過setContentView(new CustomView());,但如果我想使用MVVM設計模式,這是不行的。 我該如何區分這一點,並使其與MVVM數據綁定一起工作? 我不明白如何以及在哪裏設置CustomView,以便可以通過數據綁定查看。

請耐心等待,我是Android新手,沒有足夠的經驗。 謝謝:)

回答

3

我提出這個解決方案:

Activity.java

package com.example.myapplication; 

import android.databinding.DataBindingUtil; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import com.example.myapplication.databinding.ActivityBinding; 

import java.util.Arrays; 

public class Activity extends AppCompatActivity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     //Do magic with binding 
     ActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.activity); 
     CustomViewModel viewModel = new CustomViewModel(); 
     binding.setVariable(BR.vm, viewModel); 
     binding.executePendingBindings(); 

     //Fill model 
     viewModel.backgroundFill.set(Color.WHITE); 
     viewModel.setCircleModels(Arrays.asList(new CircleModel(0, 0), new CircleModel(200, 400))); 
    } 
} 

CustomView.java

package com.example.myapplication; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.support.annotation.ColorInt; 
import android.support.annotation.Nullable; 
import android.util.AttributeSet; 
import android.view.View; 

import java.util.Collections; 
import java.util.List; 

public class CustomView extends View 
{ 
    private Paint mPaint = new Paint(); 
    private int backgroundFill; 
    private List<CircleModel> circleModels = Collections.emptyList(); 

    public CustomView(Context context, @Nullable AttributeSet attrs) 
    { 
     super(context, attrs); 

     mPaint.setStyle(Paint.Style.FILL); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     super.onDraw(canvas); 

     mPaint.setColor(backgroundFill); 
     canvas.drawPaint(mPaint); 

     mPaint.setColor(Color.BLUE); 
     for(CircleModel model : circleModels) 
      canvas.drawCircle(model.getX(), model.getY(), 100, mPaint); 
    } 

    public void setBackgroundFill(@ColorInt int backgroundFill) 
    { 
     this.backgroundFill = backgroundFill; 
    } 

    public void setCircles(List<CircleModel> circles) 
    { 
     circleModels = circles; 
    } 
} 

CustomViewModel.java

package com.example.myapplication; 

import android.databinding.BaseObservable; 
import android.databinding.Bindable; 
import android.databinding.ObservableInt; 

import java.util.ArrayList; 
import java.util.List; 

public class CustomViewModel extends BaseObservable 
{ 
    public final ObservableInt backgroundFill = new ObservableInt(); 
    @Bindable 
    private List<CircleModel> circleModels = new ArrayList<>(); 

    public List<CircleModel> getCircleModels() 
    { 
     return circleModels; 
    } 

    public void setCircleModels(List<CircleModel> circleModels) 
    { 
     this.circleModels = circleModels; 
     notifyPropertyChanged(BR.circleModels); 
    } 
} 

CircleModel.java

public class CircleModel 
{ 
    private int x; 
    private int y; 

    public CircleModel(int x, int y) 
    { 
     this.x = x; 
     this.y = y; 
    } 

    public int getX() { return x; } 

    public int getY() { return y; } 
} 

activity.xml

<?xml version="1.0" encoding="utf-8"?> 
    <layout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <data> 

     <variable 
      name="vm" 
      type="com.example.myapplication.CustomViewModel" /> 
    </data> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <com.example.myapplication.CustomView 
      android:id="@+id/canvas" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:backgroundFill="@{vm.backgroundFill}" 
      app:circles="@{vm.circleModels}"/> 
      <!--Setters in CustomView--> 
      <!--app:backgroundFill="@{vm.backgroundFill}"--> 
      <!--app:circles="@{vm.circleModels}"--> 

    </android.support.design.widget.CoordinatorLayout> 
</layout> 

與我聯繫,如果需要的所有項目

+0

Привет!謝謝,我會嘗試這個解決方案並讓你知道結果。 – hogar

+0

你好Виталий,你可以給我整個項目的git回購,這會非常有幫助嗎?另外,你能告訴我可以在CustomView類中實現一些繪圖服務的實例化對象嗎? 謝謝 – hogar

+1

嗨,Hogar,項目鏈接:https://github.com/Arigar/AndroidMVVMExample.git。我不理解關於「某些繪圖服務的實例化對象」的問題。你的意思是[android服務](https://developer.android.com/guide/components/services.html),還是別的? –