【问题标题】:Change another content with navigation drawer使用导航抽屉更改其他内容
【发布时间】:2014-02-10 14:43:57
【问题描述】:

我使用 android studio 创建了默认的抽屉式导航应用程序。

然后我不想用不同的组件创建不同的布局。如何更改与菜单点击相关的内容。

我想在点击导航菜单链接时更改这两个内容。

虚拟内容(!impotent:内容主体是传出的,不能使用可重复使用的片段)

【问题讨论】:

    标签: android android-activity android-studio android-fragmentactivity navigation-drawer


    【解决方案1】:

    我想用不同的组件创建不同的布局。我该怎么做 更改与菜单点击相关的内容。

    => 我完全明白您的意思,即每当用户从导航抽屉中选择任何项目时,您都希望更改内容,即布局(活动)。

    如果是这种情况,那么您必须用您想要显示的新片段替换当前片段。

    例如:

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            selectItem(position);
        }
    }
    
    /** Swaps fragments in the main content view */
    private void selectItem(int position) {
        // Create a new fragment and specify the planet to show based on position
        Fragment fragment = new PlanetFragment();
        Bundle args = new Bundle();
        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        fragment.setArguments(args);
    
        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.content_frame, fragment)
                       .commit();
    
        // Highlight the selected item, update the title, and close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }
    
    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }
    

    详细示例:http://developer.android.com/training/implementing-navigation/nav-drawer.html

    【讨论】:

      【解决方案2】:

      如果我正确理解您的问题,您可以将 FrameLayout 作为容器。在列表项上单击向容器添加或替换适当的片段。

      如果您需要开始一项新活动,您也可以这样做。

      你可以按照例子@

      http://developer.android.com/training/implementing-navigation/nav-drawer.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多