与动态设置内容视图的数据绑定
data binding with dynamic set content view
我有一个导航抽屉,需要跨两个屏幕(第一、第二)使用。为此,我创建了 BaseActivity class,它将具有导航抽屉实现和一种动态设置 activity 内容的方法,如下所示。
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() 方法将动态更新 activity 内容。
有了这个,我想为第一个屏幕设置数据绑定,但我无法做到。这是我的代码
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 作为主要内容,没有导航抽屉。
谁能帮我解决这个问题?提前致谢。
去掉
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)
您正在使用 DataBindingUtil 扩充布局,但未在 FrameLayout 中进行设置,
不要使用
updateLayout(R.layout.content_first);
与其这样,不如这样做 -
ContentFirstBinding mBinding = DataBindingUtil.setContentView(this,R.layout.content_first);
navigationView.addView(mBinding.getRoot())
我有一个导航抽屉,需要跨两个屏幕(第一、第二)使用。为此,我创建了 BaseActivity class,它将具有导航抽屉实现和一种动态设置 activity 内容的方法,如下所示。
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() 方法将动态更新 activity 内容。
有了这个,我想为第一个屏幕设置数据绑定,但我无法做到。这是我的代码
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 作为主要内容,没有导航抽屉。
谁能帮我解决这个问题?提前致谢。
去掉
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)
您正在使用 DataBindingUtil 扩充布局,但未在 FrameLayout 中进行设置,
不要使用
updateLayout(R.layout.content_first);
与其这样,不如这样做 -
ContentFirstBinding mBinding = DataBindingUtil.setContentView(this,R.layout.content_first);
navigationView.addView(mBinding.getRoot())