百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

MVVMLight

> 编程语言
开源

一个工具包帮助构建 Android MVVM 应用

1.8K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个工具包帮助构建 Android MVVM 应用

MVVM Light Toolkit


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##

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

requires at least android gradle plugin 1.5.0.

##Usage##

####中文文档:MVVM Light Toolkit使用指南#### ####引申阅读:如何构建Android MVVM应用程序####

###Data Binding###

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

xml
<ImageView
  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).

java
  public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
   }

Example


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.

xml
 <android.support.v7.widget.RecyclerView
    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

java
 public final ObservableList<ViewModel> itemViewModel = new ObservableArrayList<>();
 public final ItemView itemView = ItemView.of(BR.viewModel, R.layout.layoutitem_list_view);

Adapter,ViewHolder ..is Not Required:


Example


Other attributes supported:

  • ImageView

    xml
    <attr name="uri" />
    
    <attr name="request_width" format="integer" />
    
    <attr name="request_height" format="integer" />
    <attr name="placeholderImageRes" format="reference|color" />
  • ListView、ViewPager、RecyclerView

    xml
    
    <attr name="itemView" />
    
    <attr name="items" />
    
    <attr name="adapter" />
    <attr name="dropDownItemView" format="reference" />
    <attr name="itemIds" format="reference" />
    <attr name="itemIsEnabled" format="reference" />
    
    <attr name="pageTitles" format="reference" />
  • ViewGroup

    xml
    
    <attr name="itemView" />
    
    <attr name="viewModels" format="reference" />
  • EditText

    xml
    
    <attr name="requestFocus"  format="boolean" />
  • SimpleDraweeView

    xml
    
    <attr name="uri" />
  • WebView

    xml
    
    <attr name="render" format="string" />

###Command Binding###

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

xml
   <android.support.v7.widget.RecyclerView
        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.

java
   public final ReplyCommand<Integer> loadMoreCommand = new ReplyCommand<>(
      (count) -> {
          /*count: count of list items*/
           int page=count / LIMIT +1;
           loadData(page)
      });

Example


Deal with click event of View is more convenient:

xml
 <Button
            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.

java
 public ReplyCommand btnClickCommand = new ReplyCommand(() -> {
       do something...
    });

Example


onRefreshCommand to SwipeRefreshLayout


More command binding is supported:

  • View

    xml
    
    <attr name="clickCommand" format="reference" />
    
    <attr name="onFocusChangeCommand" format="reference" />
    
    <attr name="onTouchCommand" />
  • ListView、RecyclerView

    xml
    
    <attr name="onScrollStateChangedCommand" />
    
    <attr name="onScrollChangeCommand" />
    
    <attr name="onLoadMoreCommand" format="reference" />
  • ViewPager

    xml
    
    <attr name="onPageScrolledCommand" format="reference" />
    
    <attr name="onPageSelectedCommand" format="reference" />
    
    <attr name="onPageScrollStateChangedCommand" format="reference" />
  • EditText

    xml
    
    <attr name="beforeTextChangedCommand" format="reference" />
    
    <attr name="onTextChangedCommand" format="reference" />
    
    <attr name="afterTextChangedCommand" format="reference" />
  • ImageView

    xml
     
     <attr name="onSuccessCommand" format="reference" />
     
     <attr name="onFailureCommand" format="reference" />
  • ScrollView、NestedScrollView

    xml
    
    <attr name="onScrollChangeCommand" />
    
    <attr name="onScrollChangeCommand" />
  • SwipeRefreshLayout

    xml
    
    <attr name="onRefreshCommand" format="reference" />

###Messenger###

simplifies the communication between ViewModel(major) or any components


Example


  • global message broadcast without deliver data

    java
    /* 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)

    java
     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)

    java
    Messenger.getDefault().sendToTarget(T message, R target)
    Messenger.getDefault().sendNoMsgToTargetWithToken(Object token,R target)
    Messenger.getDefault().sendNoMsgToTarget(Object target)
  • cancel register

    java
     Messenger.getDefault().unregister(Object recipient)"
     /* Usually Usage*/
     @Override
     protected void onDestroy() {
             super.onDestroy();
             Messenger.getDefault().unregister(this);
      }

License

…

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Java

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言