Android MVVM Design Pattern Examples

2019-01-16 00:19发布

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?

14条回答
家丑人穷心不美
2楼-- · 2019-01-16 00:43

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.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-16 00:47

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.

查看更多
4楼-- · 2019-01-16 00:48

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

查看更多
Juvenile、少年°
5楼-- · 2019-01-16 00:51

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.
查看更多
手持菜刀,她持情操
6楼-- · 2019-01-16 00:55

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

查看更多
该账号已被封号
7楼-- · 2019-01-16 00:58

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.

查看更多
登录 后发表回答