【问题标题】:How to have a navigation view load in multiple activities without bugs?如何在没有错误的情况下在多个活动中加载导航视图?
【发布时间】:2016-02-07 07:30:05
【问题描述】:

我创建了一个导航视图(注意:较新的导航视图,而不是设置不同的旧导航抽屉)遵循找到的指南 here。我将它设置为打开一个新活动,我只是再次重复代码但更改了类和意图语句以反映当前活动。 (更改的主要活动扩展到activity2 扩展等)但是当我这样做时,主要活动中的文本视图也会显示在activity2 中,并忽略activity2 中的textview

那么我该如何设置它以在我的所有活动中显示导航视图而无需复制代码数十次并且每个活动都有自己的textview?我也是一个新手 android 程序员,所以说“应该使用片段”或类似的东西,我需要代码示例来真正理解我需要做什么(同样,请不要让我参考导航抽屉示例,因为这是设置的不同)

【问题讨论】:

    标签: android android-layout android-activity android-studio android-navigationview


    【解决方案1】:

    我建议你使用简单的方案——一个Activity,多个Fragments

    例如 - 您可以使用 Navigation Drawer ActivityAndroid 工作室 中创建新项目(尽管在新版本的 Android 工作室 活动中会显示“导航抽屉”使用NavigationView 创建) - 它将为您创建一个MainActivity

    为了支持Fragments 方案,您需要在您的Activity 上使用特殊容器来存储此片段的内容。让我们将FrameLayout 添加到layout/content_main.xml 文件中:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".MainActivity"
        tools:showIn="@layout/app_bar_main">
    
        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>
    

    请注意,我使用content_frame id 创建了FrameLayout - 我们稍后会使用它。

    然后,例如,创建两个空白片段 - BlankFragment1BlankFragment2

    现在查看MainActivity 中的代码,而不是onNavigationItemSelected 方法。默认情况下,它看起来像:

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
    
        if (id == R.id.nav_camara) {
            // Handle the camera action
        } 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;
    }
    

    每个if 语句都帮助我们区分导航视图中的菜单项。让我们将此代码更改为通过单击R.id.nav_camara 转到BlankFragment1 并通过单击R.id.nav_gallery 转到BlankFragment2

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
    
        Fragment fragment;
        switch (item.getItemId()) {
            case R.id.nav_camara:
                fragment = BlankFragment1.newInstance("param1", "param2");
                break;
    
            case R.id.nav_gallery:
                fragment = BlankFragment2.newInstance("param3", "param4");
                break;
    
            default:
                fragment = BlankFragment1.newInstance("param1", "param2");
        }
    
        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment)
                .commit();
    
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    

    然后启动应用程序。现在,您可以通过单击这两个图标(相机和画廊)来更改片段。希望对你有帮助。

    【讨论】:

    • 好吧,我做到了,并摆脱了我在做这件事时创建的几个导致红色的问题。现在,当我尝试在应用程序中选择一个选项时,它崩溃了,我得到 info.test.MainActivity@31bd4c23 must implement OnFragmentInteractionListener
    • 请提供您的代码。我认为,您对片段和活动使用了非法类。您应该使用 AppCompactActivityandroid.support.v4.app.Fragment 类。
    • 问题是我需要添加 newIstance 和相关信息来告诉 switch 语句正确加载片段。我只有 oncreateview 来扩充 xml,我的片段中没有其他内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多