导航抽屉默认片段 [英] Navigation drawer default fragment

查看:67
本文介绍了导航抽屉默认片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手开发人员,正在将我的应用程序中的导航抽屉与android-support v7集成在一起,我有一个问题.当我启动应用程序时,主要布局是这样:

I am novice developer and I´m integrating Navigation drawer in my app with android-support v7 and I have one question. When I start the app the main layout is this:

<?xml version="1.0" encoding="utf-8"?>

<!-- The main content view -->

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- The navigation drawer -->

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

这是我的主要活动:

drawerList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Fragment fragment = null;

            switch (position) {
                case 0:
                    fragment = new Fragment1();
                    break;
                case 1:
                    fragment = new Fragment2();
                    break;
                case 2:
                    fragment = new Fragment3();
                    break;  
                case 3:
                    fragment = new Fragment4();
                    break;
            }

            FragmentManager fragmentManager = 
                    getSupportFragmentManager();

            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, fragment)
                    .commit();

            drawerList.setItemChecked(position, true);

            tituloSeccion = opcionesMenu[position];
            getSupportActionBar().setTitle(tituloSeccion);

            drawerLayout.closeDrawer(drawerList);
        }
    });

如何设置默认片段,例如应用程序的主布局?谢谢

How can I set default fragment like main layout of the app? Thank you

推荐答案

如果可以在每次创建活动时加载默认片段,可以在中放置 FragmentTransaction > onCreate()

If it is ok for you to load the default fragment every time your activity is created, you can put a FragmentTransaction in onCreate()

看起来像这样:

@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
    tx.replace(R.id.content_frame, new Fragment1());
    tx.commit();


}

如果您想要更复杂的方法(例如,回到主活动时切换到另一个片段),则可以使用 Intent ,并在 onCreate(),在加载额外的代码后,您只需将默认片段放在 defaultValue 中:

If you want a more sophisticated way of doing this (for example switching to a different fragment when you go back to the main activity), you can use an Intent with extras determining the fragment in onCreate(), where you just put your default fragment in the defaultValue upon loading the extra:

int position = getIntent().getIntExtra("position", 1);
switch(position){

    ...

}

这篇关于导航抽屉默认片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆