android - 与动态集内容视图的数据绑定
问题描述
我有一个导航抽屉,需要在两个屏幕(第一个,第二个)上使用。为此,我创建了 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 显示为没有导航抽屉的主要内容。
谁能帮我解决这个问题?提前致谢。
解决方案
摆脱
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)
推荐阅读
- angular - Why does the selected attribute work for each ng-container?
- maven - 在 maven 中通过的 intelij 失败测试
- r - 找出以 x 为因数的范围内的所有数字?
- java - “可恢复错误”接口是否比处理多个“捕获”块中的恢复更有效?
- r - 有没有办法为正交/ TLS 回归模型绘制置信区间?
- php - 扩展 ArrayObject 的对象的 PhpStorm 中的类型提示
- c++ - 从抽象数据类型转换为泛型类型
- python - 如何将 2d Python 列表转换为 2d C++ 向量?
- sql - 如何在字符串的已知部分之后选择所有内容
- python - PyQt5 信号在两个类之间发射