其他分享
首页 > 其他分享> > android-使用MVVM实现自定义视图

android-使用MVVM实现自定义视图

作者:互联网

我已经找到了android data binding with a custom view的解释,但这对我不起作用,因为我对此不太了解,而且情况有些不同.

我的点子:
我需要帆布,所以我可以在上面画些东西.
我做了一个扩展View类的类(CustomView).在CustomView类中,我创建了负责绘制的服务实例,在覆盖的onDraw方法中,我将画布传递给了服务类,以便应用程序可以进行绘制.

问题:
在活动中,我使用了setContentView(new CustomView());,但是如果我要使用MVVM设计模式,则此方法将无效.
如何分离这些并使其与MVVM数据绑定一起使用?
我不了解如何以及在何处设置CustomView,以便可以通过具有数据绑定的视图来获取/绑定CustomView?

请多多包涵,我是Android新手,没有足够的经验.
谢谢 :)

解决方法:

我提出以下解决方案:

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>

如果需要所有项目,请与我联系

标签:android-databinding,view,android,mvvm
来源: https://codeday.me/bug/20191111/2019401.html