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

The compile 'com. Kelin. Mvvmlight: library: 1.0.0'Copy the code

Usage


Chinese document:MVVM Light Toolkit Usage Guide

Extended reading:How do I build Android MVVM applications

Data Binding

   Copy the code

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


Example


    Copy the code

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);Copy the code

Adapter, ViewHolder.. is Not Required:


Example


Other attributes supported:

  • ImageView
     
     
     
     
     
     Copy the code

  • ListView,ViewPager,RecyclerView
     
     
     
     
     
     
     
     
     
     
     Copy the code

  • ViewGroup
     
     
     
     Copy the code

  • EditText
     
     Copy the code

  • SimpleDraweeView
     
     Copy the code

  • WebView
     
     Copy the code

Command Binding


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

      Copy the code

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)
        });Copy the code


Example


Deal with click event of View is more convenient:

   Copy the codeCopy the code

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

   public ReplyCommand btnClickCommand = new ReplyCommand(() -> {
         do something...
      });Copy the code


Example


onRefreshCommand to SwipeRefreshLayout


More command binding is supported:

  • View
    
    
    
    ReplyCommand would has params which means if view hasFocus.-->
    
    
    Copy the code

  • ListView,RecyclerView
    
    
    
    
    
    Copy the code

  • ViewPager
    
    
    
    
    
    Copy the code

  • EditText
    
    
    
    
    
    Copy the code

  • ImageView
     
     
     
     Copy the code

  • ScrollView,NestedScrollView
    
    
    
    Copy the code

  • SwipeRefreshLayout
    
    Copy the code

Messenger


simplifies the communication between ViewModel(major) or any components


Example


  • 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 an 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, () -> { });Copy the code

  • 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) -> { });Copy the code

  • send to specify target (inactive)
     Messenger.getDefault().sendToTarget(T message, R target)
     Messenger.getDefault().sendNoMsgToTargetWithToken(Object token,R target)
     Messenger.getDefault().sendNoMsgToTarget(Object target)Copy the code

  • cancel register
      Messenger.getDefault().unregister(Object recipient)"
      /* Usually Usage*/
      @Override
      protected void onDestroy() {
              super.onDestroy();
              Messenger.getDefault().unregister(this);
       }Copy the code

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.
Copy the code