我有一个导航抽屉,需要在两个屏幕(第一,第二)上使用。为此,我创建了BaseActivity类,该类将具有导航抽屉实现和以下一种动态设置活动内容的方法。
public class BaseActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
protected View mainView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
protected void updateLayout(int layoutId){
FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame); //Remember this is the FrameLayout area within your activity_main.xml
mainView = getLayoutInflater().inflate(layoutId, contentFrameLayout);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_first) {
Intent intent = new Intent(this, FirstActivity.class);
startActivity(intent);
} else if (id == R.id.nav_second) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
updateLayout()方法将动态更新活动内容。
这样,我想为无法执行的第一个屏幕设置数据绑定。这是我的代码
public class FirstActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
updateLayout(R.layout.content_first);
EmployBean bean = new EmployBean("100", "Rajesh");
ContentFirstBinding mBinding = DataBindingUtil.setContentView(this, R.layout.content_first);
EmployViewModel employViewModel = ViewModelProviders.of(this).get(EmployViewModel.class);
employViewModel.getEmployBean().setValue(bean);
mBinding.setEmployViewModel(employViewModel);
}
}
这会将content_first作为主要内容显示,而没有导航抽屉。
有人可以帮我解决这个问题吗?预先感谢。
答案 0 :(得分:2)
摆脱
updateLayout(R.layout.content_first);
并更改
ContentFirstBinding mBinding = DataBindingUtil.setContentView(this, R.layout.content_first);
使用
ContentFirstBinding mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.content_first, (FrameLayout) findViewById(R.id.content_frame), true)
最后一行将为您填充布局,并将其内容添加到(FrameLayout) findViewById(R.id.content_frame)
答案 1 :(得分:0)
您正在使用DataBindingUtil扩展布局,但未在FrameLayout中对其进行设置,
不要使用
updateLayout(R.layout.content_first);
相反,请执行此操作-
ContentFirstBinding mBinding = DataBindingUtil.setContentView(this,R.layout.content_first);
navigationView.addView(mBinding.getRoot())