Android MVVM Design Pattern Examples

后端 未结 14 1224
逝去的感伤
逝去的感伤 2020-12-07 07:09

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 k

相关标签:
14条回答
  • 2020-12-07 07:55

    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

    0 讨论(0)
  • 2020-12-07 07:59

    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!

    0 讨论(0)
提交回复
热议问题