初到公司实习的一些心得

2017-08-28  本文已影响0人  跑调怪蜀黍

来公司也有好几天了,这是我初次进入公司实习,虽然上班的生活跟想象中还是有些差距,但是过得还算挺充实(早上也能睡个不算太懒的懒觉……2333)。

经过这些天的上班生活,才发觉自己以前对于android学习的知识点都太肤浅,看到公司的一些项目所运用的技术基本都是自己以前没有接触过的(ps:以前觉得自己已经用过了那么多框架什么的,到公司多半就是用这些,看来我还是太年轻)。所以为了能跟上公司的脚步,这几天对一些公司常用的技术进行了初步的学习。

这几天,初步了解了android的MVVM模式,所以再这里记录一些目前理解的东西,以免以后忘记。

1、首先说说我自己理解的MVVM:

Model:就是一些Java开发人员都非常熟悉的JavaBean,只不过这些JavaBean中的每个变量都是分别与View中每个控件的属性关联了起来,即可值改变JavaBean中变量的值来达到改变View的效果,不需要开发人员自己再去控制View。

View:以我目前看来,就是xml布局和avtivity这种能直接显示在用户眼前的就叫作所谓的View。

ViewModel:我觉得所谓ViewModel其实就是负责处理Model中的数据的值是如何变化的,而不需要去考虑这些数据改变之后会产生什么样的效果,即它只是处理逻辑,不负责表现。当数据处理完之后,View的改变交给与之关联的Model去处理,不需要ViewModel再去做什么事。

2、说说我自己理解的MVVM的用法:

首先使用MVVM模式必须要在app的build.gradle中开启databinding,其中dataBinding{enabled=true}字段要直接写在android下

但是开启databinding的前提条件是Gradle版本必须大于1.5.0,并且AndroidStudio的版本必须要在2.0.0以上,否则是不能开启databinding的,也就不能使用MVVM模式。

然后进入正题:

(1)首先,创建一个JavaBean文件,作为Model如图所示:


public  classEntry {

Stringimage;

Stringtitle;

Stringpublished;

Stringcategory;

Stringurl;

Stringcontent;

publicEntry() {

}

publicEntry(String image,String title,String published,String category,String url,String content) {

this.image=image;

this.title=title;

this.published=published;

this.category=category;

this.url=url;

this.content=content;

}

publicStringgetImage() {

returnimage;

}

public voidsetImage(String image) {

this.image=image;

}

publicStringgetTitle() {

returntitle;

}

public voidsetTitle(String title) {

this.title=title;

}

publicStringgetPublished() {

returnpublished;

}

public voidsetPublished(String published) {

this.published=published;

}

publicStringgetCategory() {

returncategory;

}

public voidsetCategory(String category) {

this.category=category;

}

publicStringgetUrl() {

returnurl;

}

public voidsetUrl(String url) {

this.url=url;

}

publicStringgetContent() {

returncontent;

}

public voidsetContent(String content) {

this.content=content;

}

public voidonItemClick(View view){

}

}

(2)创建一个xml布局文件作为View,但是写法与平时的布局文件写法稍有不同:

其中根节点要用<layout></layout>,然后下面是<data><variable></data>节点。在<variable>中指明name属性和type属性,name就是后面布局中药引用的名字,type就是对应的JaveBean。然后在</data>下面才是所常见的布局方式,其中控件的属性用@{xxx.xxx}来表示,如下:

<layout>
    <data>
        <variable
            name="entry"
            type="com.demo.bean.Entry"/>
    </data>

此时只是完成了单项绑定,若要双向绑定则还需要让JavaBean继承BaseObservable
类,并且还要在对应的get方法上加上@Bindable注解,如下:

public  class Entry extends BaseObservable{
    String image;
    String title;
    String published;
    String category;
    String url;
    String content;

    public Entry() {
    }

    public Entry(String image, String title, String published, String category, String url, String content) {
            this.image=image;
            this.title=title;
            this.published=published;
            this.category=category;
            this.url=url;
            this.content=content;
    }

    @BindingAdapter("bind:image")
    public static void loadImage(RoundedImageView imageView, String image){
        Log.i("Image-------->",image);
        Glide.with(imageView.getContext()).load(image).into(imageView);
    }

    @Bindable
    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image=image;
        notifyPropertyChanged(com.demo.BR.image);
    }

    @Bindable
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title=title;
        notifyPropertyChanged(com.demo.BR.title);
    }

    @Bindable
    public String getPublished() {
        return published;
    }

    public void setPublished(String published) {
        this.published=published;
        notifyPropertyChanged(com.demo.BR.published);
    }

    @Bindable
    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category=category;
        notifyPropertyChanged(com.demo.BR.category);
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url=url;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content=content;
    }
    public void onItemClick(View view){

    }
}

若想要实现onclick方法,则只需要在JavaBean中自己添加一个方法(名字随意,如如haha()),然后在需要有onclick的控件中加上android;onclick="xxx.haha"即可:
android:onClick="@{entry.onItemClick}"

上一篇 下一篇

猜你喜欢

热点阅读