User Login (Material Design + Data-Binding + MVP architecture mode) implementation, data-bindingmvp
Reprinted please indicate the source: http://www.cnblogs.com/cnwutianhao/p/6772759.html
There are no strangers to the MVP architecture model, and Google has provided corresponding reference samples,
However, some people may wonder why the MVP architecture model written by the experts on GitHub is not the same as that of Google.
Google MVP Architecture Pattern Sample address https://github.com/googlesamples/android-architecture/tree/todo-mvp/
Next we will follow the Google Sample to achieve User Login
Project Structure:
Demo diagram:
(Upload later)
Code implementation:
1. Import necessary open source libraries
The example project adopts Material Design + Data-Binding + MVP
android { ... // Data Binding // https://developer.android.google.cn/topic/libraries/data-binding/index.html dataBinding { enabled true } }
dependencies { ... // UI compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:cardview-v7:25.3.1' compile 'com.android.support:design:25.3.1'}
2. Base Class
1) BaseActivity
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); initData(); dataProcess(); } protected abstract void initView(); protected abstract void initData(); protected abstract void dataProcess();}
2) BasePresenter
public class BasePresenter {}
3) BaseView
public interface BaseView {}
4) IBasePresenter
public interface IBasePresenter { void onDestroyView();}
3. Define a contract class to connect the P and V layers. This makes the interface clear at a glance
Here I want to explain that the most basic user logon interface needs to determine whether the user name is blank and the password is empty.
public class LoginContract { interface loginView extends BaseView { void accountIsNull(); void passWordIsNull(); void loginSuccess(); } interface loginPresenter extends IBasePresenter { void login(String account, String password); }}
4. Define a LoginPresenter. The conditions are implemented here.
public class LoginPresenter extends BasePresenter implements LoginContract.loginPresenter { private LoginContract.loginView mView; public LoginPresenter(LoginContract.loginView view) { mView = view; } @Override public void onDestroyView() { mView = null; } @Override public void login(String account, String password) { if (TextUtils.isEmpty(account)) { mView.accountIsNull(); return; } if (TextUtils.isEmpty(password)) { mView.passWordIsNull(); return; } mView.loginSuccess(); }}
5. Define a LoginActivity as the View layer.
Public class LoginActivity extends BaseActivity implements LoginContract. loginView {private Context mContext; private LoginPresenter mLoginPresenter; private ActivityLoginBinding mBinding; @ Override protected void initView () {mContext = LoginActivity. this; mBinding = DataBindingUtil. setContentView (this, R. layout. activity_login) ;}@ Override protected void initData () {mLoginPresenter = new LoginPresenter (this); mBinding. loginBtn. setOnClickListener (this); mBinding. loginChangePassword. setOnClickListener (this); mBinding. loginRegister. setOnClickListener (this) ;}@ Override protected void dataProcess () {}@ Override public void onClick (View v) {switch (v. getId () {case R. id. login_btn: mLoginPresenter. login (mBinding. loginAccount. getText (). toString (), mBinding. loginPassword. getText (). toString (); break; default: break; }}@ Override public void accountIsNull () {Toast. makeText (mContext, "enter your account", Toast. LENGTH_LONG ). show () ;}@ Override public void passWordIsNull () {Toast. makeText (mContext, "enter your password", Toast. LENGTH_LONG ). show () ;}@ Override public void loginSuccess () {Intent intentRegister = new Intent (); intentRegister. setClass (LoginActivity. this, MainActivity. class); startActivity (intentRegister); overridePendingTransition (android. r. anim. fade_in, android. r. anim. fade_out); finish ();}}
6. layout code
Layout (Data-Binding), CardView (Material Design), TextInputLayout (Material Design)
<? Xml version = "1.0" encoding = "UTF-8"?> <Layout xmlns: android = "http://schemas.android.com/apk/res/android"> <RelativeLayout xmlns: app = "http://schemas.android.com/apk/res-auto" android: id = "@ + id/activity_main" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "@ drawable/splash_image"> <android. support. v7.widget. cardView style = "@ style/cardElevation" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: layout_marginLeft = "16dp" android: layout_marginRight = "16dp" app: cardCornerRadius = "7dp"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView style = "@ style/TextStyle. heading "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_gravity =" center | top "android: layout_marginTop =" 40dp "android: text = "Logon account" android: textAllCaps = "true" android: textSize = "20sp"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_gravity = "center" android: layout_margin = "20dp" android: orientation = "vertical"> <android. support. design. widget. textInputLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "Account" android: textColorHint = "@ color/gray" app: hintTextAppearance = "@ style/TextAppearance. app. textInputLayout "> <android. support. design. widget. textInputEditText android: id = "@ + id/login_account" style = "@ style/TextStyle" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginLeft = "20dp" android: layout_marginRight = "20dp" android: background = "@ drawable/input_border_bottom" android: cursorVisible = "true" android: gravity = "center | left | bottom" android: inputType = "textEmailAddress" android: maxLength = "50" android: paddingBottom = "10dp" android: textColor = "@ color/black_effective" android: textSize = "18sp"/> </android. support. design. widget. textInputLayout> <android. support. design. widget. textInputLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "password" android: textColorHint = "@ color/gray" app: hintTextAppearance = "@ style/TextAppearance. app. textInputLayout "app: passwordToggleEnabled =" true "> <android. support. design. widget. textInputEditText android: id = "@ + id/login_password" style = "@ style/TextStyle" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginLeft = "20dp" android: layout_marginRight = "20dp" android: layout_marginTop = "30dp" android: background = "@ drawable/input_border_bottom" android: cursorVisible = "true" android: gravity = "center | left | bottom" android: inputType = "textPassword" android: maxLength = "50" android: paddingBottom = "10dp" android: textColor = "@ color/black_effective" android: textSize = "18sp"/> </android. support. design. widget. textInputLayout> <Button android: id = "@ + id/login_btn" style = "@ style/Button. primary "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: layout_gravity =" center "android: layout_marginLeft =" 15dp "android: layout_marginRight =" 15dp "android: layout_marginTop = "20dp" android: padding = "10dp" android: text = "login" android: textSize = "18dp"/> </LinearLayout> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_margin = "35dp"> <TextView android: id = "@ + id/login_change_password" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: text = "Change Password"/> <TextView android: id = "@ + id/login_register" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentRight = "true" android: text = "register account"/> </RelativeLayout> </LinearLayout> </android. support. v7.widget. cardView> </RelativeLayout> </layout>
Sample download: Material Design logon Interface
7. Summary
As an alternative product of the MVC Architecture Model, the MVP architecture model is the trend of Android development today. Google recommends developers to use this mode, and there is no reason for developers to reject it.
I think 99.9% of the Android phones produced now are Android 5.0 +, so developers should learn more about Material Design. Instead of making a page like Android 2.x or even 1.x.
Data-Binding is a product that Google recommends developers to use to replace findViewById.
In a word, Google officially recommends that developers use key technologies in apps sooner or later. If you don't use them, you will be eliminated.