Ad
Data Binding With Dynamic Set Content View
I am having a navigation drawer which needs to be used across two screens(First, Second). For this, I have created BaseActivity class which will have the navigation drawer implementation and one method to set the activity content dynamically as below.
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() method will update the activity content dynamically.
With this, I want to set data binding for the first screen which I am not able to do. Here is my code
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);
}
}
This is displaying the content_first as the main content without navigation drawer.
Can anyone help me how to solve this? Thanks in advance.
Ad
Answer
Get rid of
updateLayout(R.layout.content_first);
and change
ContentFirstBinding mBinding = DataBindingUtil.setContentView(this, R.layout.content_first);
with
ContentFirstBinding mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.content_first, (FrameLayout) findViewById(R.id.content_frame), true)
the last line will take care of inflate the layout for you, and add its content to (FrameLayout) findViewById(R.id.content_frame)
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad