简体   繁体   English

带片段的 Android Studio 导航抽屉

[英]Android studio navigation drawer with fragment

Hello i'm not an expert in android development.你好,我不是安卓开发专家。 I'm trying to develop an application using the Navigation drawer template of Android Studio.我正在尝试使用 Android Studio 的导航抽屉模板开发应用程序。 So, i created a new project using this template.所以,我使用这个模板创建了一个新项目。 But when i run the program an click on a menu item, the view doesn't change.但是当我运行程序并单击菜单项时,视图不会改变。 So i searched everywhere on internet i didn't see how i can handle this.所以我在互联网上到处搜索我没有看到我如何处理这个。 This is the code provided by studio这是studio提供的代码

public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.

int id = item.getItemId();

if (id == R.id.nav_camara) {


} else if (id == R.id.nav_gallery) {

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_manage) {

} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;

} }

What i want to achieve is to replace the current view with the appropriate view which menu's is cliked.我想要实现的是用点击菜单的适当视图替换当前视图。

Create Gallery.Java and paste this code:创建 Gallery.Java 并粘贴以下代码:

public class Gallery extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_gallery,null);
    }


}

And in your navigation drawer file paste this code:并在您的导航抽屉文件中粘贴此代码:

 if (id == R.id.nav_gallery)
        {
            Fragment fragment = new Gallery();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.framelayout, fragment).commit();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM