首页 > 解决方案 > 与动态集内容视图的数据绑定

问题描述

我有一个导航抽屉,需要在两个屏幕(第一个,第二个)上使用。为此,我创建了 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 显示为没有导航抽屉的主要内容。

谁能帮我解决这个问题?提前致谢。

标签: androidandroid-databindingandroid-mvvm

解决方案


摆脱

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)


推荐阅读