DataBinding: How to use BaseActivity / How to use Abstraction

老子叫甜甜 提交于 2019-12-05 02:29:25

Might be late for this question.

Your BaseActivity should looks like following.

public abstract class BaseActivity<B extends ViewDataBinding, T extends BaseViewModel> extends AppCompatActivity {

    protected B dataBinding;
    protected T baseViewModel;

    protected void bindView(int layoutId) {
        dataBinding = DataBindingUtil.setContentView(this, layoutId);
    }

    @Override
    protected void onDestroy() {
        baseViewModel.detachView();
        super.onDestroy();
    }
}

Your MainActivity should looks like following.

public class MainActivity extends BaseActivity<ActivityMainBinding, MainActivityViewModel> implements MainActivityView, View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bindView(R.layout.activity_main);

        baseViewModel = new MainActivityViewModel();
        baseViewModel.attachView(this);

        dataBinding.btnPerformOperation.setOnClickListener(this);

    }

    @Override
    public void OnDataLoad(String item) {
        dataBinding.setIsLoading(false);
        startActivity(new Intent(MainActivity.this, SecondActivity.class).putExtra("result", item));
    }

    @Override
    public void OnError(Throwable throwable) {

    }

    @Override
    public void onClick(View v) {
        dataBinding.setIsLoading(true);
        baseViewModel.loadNextData();
    }
}

Reference source code can be download from here.

Hope this helps you.

how about this?

BaseActivityViewModel.java

public class BaseActivityViewModel {
  public void onClick() {
    Log.i("BaseActivityViewModel | onClick", "111111111111");
  }
}

activity_base.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

  <data>

    <variable
      name="model"
      type="kr.changhoonjin.textmvvmwithextend.BaseActivityViewModel"/>
  </data>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:onClick="@{()->model.onClick()}"
      android:text="base"/>

    <FrameLayout
      android:id="@+id/layout_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

  </LinearLayout>
</layout>

BaseActivity.java

public class BaseActivity extends AppCompatActivity {
  private ActivityBaseBinding binding;

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_base);
    binding.setModel(new BaseActivityViewModel());
  }

  protected <T extends ViewDataBinding> T putContentView(@LayoutRes int resId) {
    return DataBindingUtil.inflate(getLayoutInflater(), resId, binding.layoutContainer, true);
  }
}

MainActivityViewModel.java

public class MainActivityViewModel {
  public void onClick2() {
    Log.i("MainActivityViewModel | onClick2", "2222222222222");
  }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

  <data>

    <variable
      name="model"
      type="kr.changhoonjin.textmvvmwithextend.MainActivityViewModel"/>
  </data>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello World!"/>

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:onClick="@{()->model.onClick2()}"
      android:text="main"/>

  </LinearLayout>
</layout>

MainActivity.java

public class MainActivity extends BaseActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainBinding binding = putContentView(R.layout.activity_main);
    binding.setModel(new MainActivityViewModel());
  }
}

It will be same as you have done in BaseActivity, instead of inflate just use setContentView

private ActivityCardRecyclerviewBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this,R.layout.activity_card_recyclerview);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!