I\'m using MVVM framework for Android application with data-binding library.
I have some reusable compoments, that should have every activity. f.e. toolbar, menu, floati
Here is my solution,hope it will help you,I do not think it's a good way.If you find a better way,please tell me .thanks!
Although data-binding library does not allow to include layout dynamically,we can use different ViewModel for different layout .I used a LinearLayout as a container to host dynamic layout,then override Activity setContentView method to add view to my container(LinearLayout).
GenericActivity
protected GenericViewBinding mBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.generic_view);
}
@Override
public void setContentView(View view) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mBinding.container.addView(view,lp);
}
generic_view layout
<data>
<variable
name="genericViewModel"
type=".viewModel.GenericViewModel"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<!--a custom view,some generic layout -->
<.view.base.BaseTitleView
android:id="@+id/base_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/base_title"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
In concrete Activity you use it like this:
ConcreteActivity
//different layout use different ViewModel here,generic logic&layout is in GenericActivity&generic_view layout.
private ActivityConcreteBinding mActivityConcreteBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivityConcreteBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_concrete,null,false);
setContentView(mActivityConcreteBinding.getRoot());
ConcreteViewModel concreteViewModel = new ConcreteViewModel(mBinding);//mBinding from GenericActivity, deliver it into ConcreteViewModel to do some generic logic.
...
}
For me ,no need to copy paste generic layout for every activity any more.