我正在使用片段并成功设置了所有活动。一切都功能齐全。
我的问题是,当应用程序最初启动时,我似乎无法加载默认布局或片段类。
我尝试了一些奇怪的结果。这是我MainActivity
的代码。
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@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);
}
@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) {
int id = item.getItemId();
Fragment fragment = null;
if (id == home) {
fragment = new Home();
} else if (id == services) {
fragment = new Services();
} else if (id == beforeAfter) {
fragment = new BeforeAfter();
} else if (id == staff) {
fragment = new Staff();
} else if (id == patientInfo) {
fragment = new Patient();
} else if (id == blog) {
fragment = new Blog();
} else if (id == contactUs) {
fragment = new Contact();
}
if (fragment != null) {
android.support.v4.app.FragmentManager fragmentManager =
getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.screen_area, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
这是Home.class的片段代码
public class Home extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.home, null);
}
}
编辑我的MainActivity
java文件并附加onCreate
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.screen_area, new Home());
ft.commit();
它现在有效。我想我正在分配Home片段的XML布局页面的名称,而不是content_main下的FrameLayout的ID。
答案 0 :(得分:0)
你的代码看起来很好。我想问题出在你的activity_main.xml
确保DrawerLayout
内只有两个孩子
一个用于主容器,另一个用于NavigationView
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/my_navigation_items" />
</android.support.v4.widget.DrawerLayout>
您需要在onCreate
FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, new YourFragment());
ft.commit(); //important
答案 1 :(得分:0)
在你的onCreate中调用片段
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, new Fragment1());
ft.commit();