Android MVVM upgrade DataBinding+Retrofit+RxJava+ViewModel+LiveData

This article mainly uses the generic optimization project structure to explain the key modifications. If you don't understand the project structure, please see my last two Blogs:

Android MVVM improved DataBinding+Retrofit+RxJava+ViewModel+LiveData (2)

Android MVVM Basic Edition DataBinding+Retrofit+RxJava (1)

I. modify BaseActivity

public abstract class BaseActivity<VM extends AndroidViewModel, DB extends ViewDataBinding> extends AppCompatActivity {
    private String TAG = "";
    protected boolean isActive;
    private WaitingAlertDialog waitDialog;

    protected VM viewModel;
    protected DB dataBinding;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        dataBinding = DataBindingUtil.setContentView(this,getLayoutId());

        Class<VM> viewModelClass = ClassUtil.getViewModel(this);
        if (viewModelClass != null) {
            viewModel = ViewModelProviders.of(this).get(viewModelClass);
        }
        initViews(savedInstanceState);
    }

    public abstract int getLayoutId();

    /**
     * Initialize views
     *
     * @param savedInstanceState
     */
    public abstract void initViews(Bundle savedInstanceState);


}

Add two generics, VM and DB inherit AndroidViewModel and ViewDataBinding respectively, add getLayoutId() method to get layout id, and initViews(Bundle savedInstanceState) method generally performs some initialization layout operations here

II. Modify MainActivity

public class MainActivity extends BaseActivity<MainViewModel, ActivityMainBinding> {
    private LinearLayoutManager layoutManager;
    private MainAdapter adapter;

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

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initViews(Bundle savedInstanceState) {
        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        dataBinding.recyclerView.setLayoutManager(layoutManager);
        adapter = new MainAdapter(this);
        dataBinding.recyclerView.setAdapter(adapter);
        showWaitDialog();//Show load animation
        viewModel.getList().observe(this, new Observer<BaseBean<List<PublicNumberBean>>>() {
            @Override
            public void onChanged(@Nullable BaseBean<List<PublicNumberBean>> listBaseBean) {
                if (listBaseBean.getErrorCode() == 0) {
                    dismissWaitDialog();//Close load graph
                    adapter.setData(listBaseBean.getData());
                }

            }
        });
    }

}

MainActivity integrates BaseActivity into its own ViewModel and DataBInding, and then directly uses their objects.

Three, summary

What I wrote is very simple. God can directly read the github project with several links written at the beginning of my first blog

Keywords: Android Retrofit github

Added by ramrod737 on Fri, 22 Nov 2019 19:58:57 +0200