1.实现抽象方法
protected abstract void configHeader(HeaderViewModel headerViewModel);
对HeaderView对象进行修改,更改头部
public abstract class HeaderAppCompatActivity extends BaseAppCompatActivity{
private LinearLayout root_layout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 重点
ActivityBaseBinding activityBaseBinding= DataBindingUtil.inflate(LayoutInflater.from(this),R.layout.activity_base,null,false);
HeaderViewModel headerViewModel = new HeaderViewModel();
configHeader(headerViewModel);
activityBaseBinding.setHeaderViewModel(headerViewModel);
super.setContentView(activityBaseBinding.getRoot());
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
setContentView(View.inflate(this, layoutResID, null));
}
@Override
public void setContentView(View view) {
root_layout = (LinearLayout) findViewById(R.id.root_layout);
if (root_layout != null) {
root_layout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
}
/**
*配置头部信息
*/
protected abstract void configHeader(HeaderViewModel headerViewModel);
}
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View"></import>
<variable
name="headerViewModel"
type="com.bilab.healthexpress.reconsitution_mvvm.base.viewModel.HeaderViewModel"></variable>
</data>
<!--头部-->
<LinearLayout
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="@{headerViewModel.headerName}"
tools:text="标题"></TextView>
</android.support.v7.widget.Toolbar>
</LinearLayout>
</layout>
public class HeaderViewModel {
public String headerName="";
public boolean rightVisible=false;
public boolean leftVisible=true;
public void leftImageOnclick(View view){
Context context= view.getContext();
if(context instanceof Activity){
Activity activity= (Activity) context;
activity.finish();
}
}
}
网友评论