1. 程式人生 > >android -------- MVP+DataBinding 的使用

android -------- MVP+DataBinding 的使用

ondestroy ast tof tab pat ins result hvie eat

今天來說說MVP+DataBinding 的使用

以一個登錄案例來講解

技術分享圖片

布局:(ConstraintLayout 作為根布局)

<layout>


    <data>

        <variable
            name="onClick"
            type="com.zhangqie.mvplogin.LoginActivity.OnViewClick" />

    </data>

    <android.support.constraint.ConstraintLayout xmlns:android
="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".LoginActivity"> <TextView
android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="45dp" android:gravity="center" android:text="賬號:" android:textColor="@android:color/black" android:textSize="16dp" app:layout_constraintBottom_toBottomOf
="parent" app:layout_constraintHorizontal_bias="0.2" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.4" /> <EditText android:id="@+id/et_name" android:layout_width="222dp" android:layout_height="45dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toRightOf="@+id/tv1" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.4" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="45dp" android:gravity="center" android:text="密碼:" android:textColor="@android:color/black" android:textSize="16dp" app:layout_constraintHorizontal_bias="0.2" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/tv1" /> <EditText android:id="@+id/et_pwd" android:layout_width="222dp" android:layout_height="45dp" app:layout_constraintLeft_toRightOf="@+id/tv2" app:layout_constraintTop_toBottomOf="@+id/et_name" /> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="25dp" android:onClick="@{onClick.OnClickCommand}" android:text="登錄" app:layout_constraintTop_toBottomOf="@+id/et_pwd" /> </android.support.constraint.ConstraintLayout> </layout>

BaseActivity.Java

public abstract class BaseActivity<D extends ViewDataBinding,V,T extends BasePresenter<V>> extends AppCompatActivity{


    protected D viewDataBinding;
    protected T p;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewDataBinding = DataBindingUtil.setContentView(this, setMainLayout());
        p = createPresenter();
        p.attachView((V)this);
        initView();
        initBeforeData();
    }


    protected abstract T createPresenter();

    /***
     * 初始化布局
     */
    protected abstract int setMainLayout();

    /**
     * 初始化View
     */
    protected abstract void initView();

    /**
     * 初始化先前數據
     */
    protected abstract void initBeforeData();

    /***
     * 跳轉Activity
     * @param mClass
     */
    protected void openActivity(Class<?> mClass) {
        openIntent(new Intent(this, mClass));
    }



    /**
     * 彈出toast 顯示時長short
     *
     * @param msg
     */
    protected void showToastShort(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }
    }
    protected void showToastShort(int msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }


    protected void openIntent(Intent intent) {
        startActivity(intent);
    }

    protected void openForResultActivity(Intent intent, int requestCode){
        startActivityForResult(intent,requestCode);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (p != null){
            p.detachView();
        }

    }

}

Activity.java

public class LoginActivity extends BaseActivity<LoginMainBinding,IView,LoginPresenter> implements IView {

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected int setMainLayout() {
        return R.layout.login_main;
    }

    @Override
    protected void initView() {
        viewDataBinding.setOnClick(new OnViewClick());
    }

    @Override
    protected void initBeforeData() {

    }

    @Override
    public void showLoading(String msg) {
        showToastShort(msg);
    }

    public class OnViewClick {
        public void OnClickCommand(View view) {
            switch (view.getId()) {
                case R.id.btn_login:
                    p.showLogin(viewDataBinding.etName.getText().toString(),viewDataBinding.etPwd.getText().toString());
                    break;
            }
        }
    }
}

效果圖:

技術分享圖片

源碼下載: https://github.com/DickyQie/android-databinding

總結:

  • 減少各層之間耦合,易於後續的需求變化,降低維護成本。

  • Presenter層獨立於Android代碼之外,可以進行Junit測試。

  • 接口和類較多,互相做回調,代碼臃腫。

  • Presenter層與View層是通過接口進行交互的,接口粒度不好控制。

有不足之處,望指正

android -------- MVP+DataBinding 的使用