我知道如何使用下面的代码使用片段来做到这一点:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment myFragment= new custProductFrag();
UserLocalStore userLocalStore;
if (id == R.id.nav_products) {
myFragment=new custProductFrag();
} else if (id == R.id.nav_shipment) {
myFragment=new custShipmentFrag();
} else if (id == R.id.nav_report) {
myFragment=new custReportFrag();
} else if (id == R.id.nav_acc) {
} else if (id == R.id.nav_logout) {
logout();
}
android.support.v4.app.FragmentManager fragmentManager= getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.contentStuff,myFragment)
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
但我希望custProduct扩展AppCompatActivity。
更新: 我不知道这是否安全但现在使用Linear Inflater对我有用 但是必须为每个子布局添加ID子项:
LayoutInflater inflater = getLayoutInflater();
LinearLayout container = (LinearLayout) findViewById(R.id.contentStuff);
UserLocalStore userLocalStore;
if (id == R.id.nav_products) {
LinearLayout child = (LinearLayout) findViewById(R.id.child);
container.removeView(child);
inflater.inflate(R.layout.cust_products, container);
} else if (id == R.id.nav_shipment) {
LinearLayout child = (LinearLayout) findViewById(R.id.child);
container.removeView(child);
inflater.inflate(R.layout.cust_shipment, container);
} else if (id == R.id.nav_report) {
LinearLayout child = (LinearLayout) findViewById(R.id.child);
container.removeView(child);
inflater.inflate(R.layout.cust_report, container);
} else if (id == R.id.nav_acc) {
} else if (id == R.id.nav_logout) {
logout();
}