android 轻量级工具,MVVMLight:一个Android MVVM 轻量级工具库

MVVM Light Toolkit

625332134c6f4d4600884b99daebf603.png

A toolkit help to build Android MVVM Application,We have more attributes for Data Binding of View(like Uri for ImageView) ,we create some command for deal with event( like click of Button),also have a global message pipe to communicate with other ViewModel. ##Download##

compile 'com.kelin.mvvmlight:library:1.0.0'

requires at least android gradle plugin 1.5.0.

##Usage##

###Data Binding###

Binding URI to the ImageView with bind:uri will make it loading bitmap from URI and render to ImageView automatically.

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentRight="true"

bind:uri="@{viewModel.imageUrl}"

bind:placeholderImageRes="@{R.drawable.ic_launcher}"/>

Fresco.initialize(this) is require,because of loading image use Fresco default).

public class MyApp extends Application {

@Override

public void onCreate() {

super.onCreate();

Fresco.initialize(this);

}

Example

625332134c6f4d4600884b99daebf603.png

AdapterView like ListView、RecyclerView 、ViewPager is convenient, bind it to the collection view with app:items and app:itemView,You should use an ObservableList to automatically update your view based on list changes.

android:layout_width="match_parent"

android:layout_height="match_parent"

bind:itemView="@{viewModel.itemView}"

bind:items="@{viewModel.itemViewModel}"

bind:layoutManager="@{LayoutManagers.linear()}"

In ViewModel define itemViewModel and itemView

public final ObservableList itemViewModel = new ObservableArrayList<>();

public final ItemView itemView = ItemView.of(BR.viewModel, R.layout.layoutitem_list_view);

Adapter,ViewHolder ..is Not Required:

Example

625332134c6f4d4600884b99daebf603.png

Other attributes supported:

ImageView

ListView、ViewPager、RecyclerView

ViewGroup

EditText

SimpleDraweeView

WebView

###Command Binding###

When RecyclerView scroll to end of list,we have onLoadMoreCommand to deal with event.

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent"

bind:onLoadMoreCommand="@{viewModel.loadMoreCommand}"/>

In ViewModel define a ReplyCommand field to deal with this event.

public final ReplyCommand loadMoreCommand = new ReplyCommand<>(

(count) -> {

/*count: count of list items*/

int page=count / LIMIT +1;

loadData(page)

});

Example

625332134c6f4d4600884b99daebf603.png

Deal with click event of View is more convenient:

android:layout_width="match_parent"

android:layout_height="wrap_content"

bind:clickCommand="@{viewModel.btnClickCommand}" />

In ViewModel define a ReplyCommand btnClickCommand will be call when click event occur.

public ReplyCommand btnClickCommand = new ReplyCommand(() -> {

do something...

});

Example

625332134c6f4d4600884b99daebf603.png

onRefreshCommand to SwipeRefreshLayout

625332134c6f4d4600884b99daebf603.png

More command binding is supported:

View

ListView、RecyclerView

ViewPager

EditText

ImageView

ScrollView、NestedScrollView

SwipeRefreshLayout

###Messenger###

simplifies the communication between ViewModel(major) or any components

Example

625332134c6f4d4600884b99daebf603.png

global message broadcast without deliver data

/* TOKEN: like Action of broadcast with who register this token will be notified when event occur.*/

Messenger.Default().sendNoMsg(TOKEN);

/*context: it usually to be a activity ,this parameter is represent to

a receiver which is mean for convenient when unregister message.

TOKEN: like Action of broadcast with who register this token will be notified when event occur.

(data)->{ }:Action to deal with event. */

Messenger.Default().register(context, TOKEN, () -> { });

global message broadcast (carry data to receiver)

Messenger.getDefault().send(data, TOKEN)

/*context:

TOKEN:

Data.class: type of deliver data.

(data)->{ }: function to deal with event which has data is deliver by sender.*/

Messenger.getDefault().register(context, TOKEN, Data.class, (data) -> { });

send to specify target (inactive)

Messenger.getDefault().sendToTarget(T message, R target)

Messenger.getDefault().sendNoMsgToTargetWithToken(Object token,R target)

Messenger.getDefault().sendNoMsgToTarget(Object target)

cancel register

Messenger.getDefault().unregister(Object recipient)"

/* Usually Usage*/

@Override

protected void onDestroy() {

super.onDestroy();

Messenger.getDefault().unregister(this);

}

License

Copyright 2016 Kelin Hong

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值