Android – using MVVM to implement custom views

I have found the explanation of Android data binding with a custom view, but it doesn't work for me because I don't know much about it and the situation is somewhat different

My idea: I need canvas, so I can draw something on it. I made a class that extends the view class (customview). In the customview class, I created the service instance responsible for drawing. In the overlaid OnDraw method, I passed the canvas to the service class so that the application can draw

Problem: in the activity, I used setcontentview (New customview());, However, if I want to use MVVM design pattern, this method will not work. How to separate these and use them with MVVM data binding? I don't know how and where to set up a customview so that I can get / bind a customview from a view with data binding?

Please forgive me. I'm a novice on Android and don't have enough experience. Thank you:)

resolvent:

I propose the following solutions:

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>

If you need all the items, please contact me

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>