I currently do a lot of WPF development and have started creating some basic Android apps. When creating WPF apps I often use MVVM, normally using Prism, and would like to know if there are any examples of MVVM for the Android platform?
问题:
回答1:
I am the developer of Android-Binding. Like @Brentley said, it's a very new project but I do hope to get more buzz and experience so that it can be improved. Back to your question, I have written some simple introduction/tutorials on MVVM with android-binding:
- Android MVVM Tutorials (with android binding)
- Introduction to Android Binding (codeproject)
- Model Validation in Android Binding (codeproject)
- Wiki in project homepage
Potential adopters please also register on the project discussion group.
回答2:
I sometimes use ViewModels to translate from a pure Model to what the Model should be displayed as, but so much of the MVVM-isms come from the fact that you have this massive data binding engine built into the WPF framework. You probably won't find the exact experience of WPF + MVVM in the Android world, but you can take a lot of the good concepts and implement them (just without the automatic data binding stuff).
For one, just create ViewModels. You don't need a framework like Prism to create ViewModels. You don't have all the PropertyChanged notifications and stuff like that, but you can translate your data into information that can be better used by your UI which will clean up your code. A perfect example of this is something I did with a slider-heavy UI. Android's SeekBar is always zero based and works with integer values, so you can't bind to min, max, and increment values from your model. You can use a ViewModel to translate your min/max values into 0-based equivalents that your SeekBar can use...just an example. Same goes for displaying colors and sizes based on value ranges, etc. To me, that's what ViewModels are all about.
As far as DependencyInjection stuff, check out RoboGuice. I just started using this in one of my projects after seeing a presentation by its creator at a local Meetup, and it's probably just what you're looking for.
RoboGuice on Google Code
RoboGuice Google Group
回答3:
There is now an Official Android Data Binding Plugin although its still in beta at the moment. Work is also being done to add tooling support for the binding syntax in the beta version of Android Studio.
See below for more information
https://developer.android.com/tools/data-binding/guide.html
回答4:
There is a relatively new framework being developed that allows for Views to be bound to ViewModels called android-binding. Using this framework and RoboGuice you can get your MVVM on. The framework still needs some work, but it's a good starting point.
回答5:
Android-Data-Binding library is a tool for connecting data to user interface elements. Once the layout file created and each item is tagged, one line of code binds all the data to user interface elements and saves your time for other tasks.
回答6:
Recently I have implemented the MVVM pattern for building an Android app with Data Binding Library. Here you can read the detailed review of the work I have done and the code fragments: http://cases.azoft.com/mvvm-android-data-binding/
To learn more about the topic, you can also have a look at these app samples: https://github.com/ivacf/archi
There are visual examples of work done with the search and list screen.
回答7:
A few years ago I also do some WPF&WP development,Prism & MVVM Light Toolkit is commonly use to build WP App,it is perfect for windows phone application architecture I think ! so I use my previous experience of WP development then imitate to create Android 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).
GitHub:Android MVVM Light Toolkit, there are samples for reference.
Architecture: http://upload-images.jianshu.io/upload_images/966283-78b410b9af8b18fa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240
hope to help you!
回答8:
Great! Articals by @Florina Muntenescu
The Model-View-ViewModel Pattern
The main players in the MVVM pattern are:
- The View — that informs the ViewModel about the user’s actions
- The ViewModel — exposes streams of data relevant to the View
- The DataModel — abstracts the data source. The ViewModel works with the DataModel to get and save the data.
Example of MVVM Architecture:
https://github.com/erikcaffrey/People-MVVM
https://github.com/googlesamples/android-architecture/tree/todo-mvvm-databinding/
https://github.com/iammert/Android-MVVM-Architecture
https://github.com/segunfamisa/android-mvvm-sample
https://github.com/manas-chaudhari/android-mvvm
回答9:
There is one project called MVVMCross.
It's free, open-source and well designed MVVM framework, developed by Stuart Lodge.
He implemented binding for Android and iPhone, so now MVVM is applicable to all of these platforms too.
For me it is one of the best MVVM frameworks - it really shows the power of MVVM. With it you can write one code (model and viewmodel layers) for different platforms (WP7, Android, iPhone, WinRT) and just change application UI (view layer).
回答10:
Just to post as a reference to other people who may be interested. I am a contributor to RoboBinding - A data-binding Presentation Model framework for the Android platform. It is another framework for the same purpose. Apart from helping with project structure, one major focus for RoboBinding is to make testing android apps with normal JUnit tests possible instead of Android unit tests, as Unit tests are so important to guarantee the quality of projects, but Android unit tests take ages to run and make unit tests somewhat impractical. RoboBinding itself comes with more than 300 JUnit tests to ensure its quality. MVVM originated from Microsoft as a specialization of the Presentation Model design pattern introduced by Martin Fowler. Other alternatives: Android-Binding, Bindroid and MvvmCross.
回答11:
You can follow these steps for DataBinding in Fragments: I have posted design and java class both in Example for Binding Data in Fragment.
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data class=".UserBinding">
<variable name="user" type="com.darxstudios.databind.example.User"/>
</data>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment"
>
<TextView android:text='@{user.firstName+" "+user.lastName}' android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_marginStart="40dp"
android:layout_marginTop="160dp" />
</RelativeLayout>
</layout>
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final User user = new User();
user.setFirstName("Michael");
user.setLastName("Cameron");
UserBinding binding = DataBindingUtil.inflate(inflater,R.layout.fragment_main, container, false);
binding.setUser(user);
View view = binding.getRoot();
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
user.setFirstName("@Darx");
user.setLastName("Val");
}
});
return view;
}
}
Developer site DataBinding Guide Line
回答12:
I found this Writing Testable Android MVVM App series written about MVVM using Android Data Binding library is really nice. In the series he explained from simple example to recyclerview, and there are tests as well.
You can maybe try the mv2m library, too.
回答13:
There are plenty of examples for MVVM framework in github. I recommend using DroidWizard
DroidWizard does its own coupling between view and view model and the framework is slightly different from mvvm.
ModelEngine ViewModel View instean of Model ViewModel View
回答14:
https://github.com/MindorksOpenSource/android-mvvm-architecture
Android MVVM Architecture: Sample App
This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava, FastAndroidNetworking, PlaceHolderView and AndroidDebugDatabase
The app has following packages:
data: It contains all the data accessing and manipulating components.
di: Dependency providing classes using Dagger2.
ui: View classes along with their corresponding ViewModel.
utils: Utility classes.