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

android-使用MVVM实现自定义视图,第1张

概述我已经找到了androiddatabindingwithacustomview的解释,但这对我不起作用,因为我对此不太了解,而且情况有些不同.我的点子:我需要帆布,所以我可以在上面画些东西.我做了一个扩展View类的类(CustomView).在CustomView类中,我创建了负责绘制的服务实例,在覆盖的onDraw方法中,

我已经找到了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-使用MVVM实现自定义视图全部内容,希望文章能够帮你解决android-使用MVVM实现自定义视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1121028.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存