NavigationDrawer onCreate空活动

时间:2013-11-22 11:50:04

标签: java android android-fragments navigation-drawer

我按照这个示例:http://javatechig.com/android/navigation-drawer-android-example在我的应用程序中创建导航抽屉。我可以创建它,但它只在我点击第一个菜单项时显示我的片段。我会直接创建onCreate我的第一个片段而不点击该项目,因为这将是另一个片段。我该怎么做?这是在单击片段时启动的导航抽屉活动:

public class MainNavDrawer extends Activity {
    // Within which the entire activity is enclosed
    private DrawerLayout mDrawerLayout;

    // ListView represents Navigation Drawer
    private ListView mDrawerList;
    public TextView textview;
    // ActionBarDrawerToggle indicates the presence of Navigation Drawer in the action bar
    private ActionBarDrawerToggle mDrawerToggle;

    // Title of the action bar
    private String mTitle = "";

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.menu_listview);

            mTitle = "Androi Reboot";
            getActionBar().setTitle(mTitle);

            // Getting reference to the DrawerLayout
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

            mDrawerList = (ListView) findViewById(R.id.drawer_list);

            // Getting reference to the ActionBarDrawerToggle
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                            R.drawable.ic_drawer, R.string.drawer_open,
                            R.string.drawer_close) {

                    /** Called when drawer is closed */
                    public void onDrawerClosed(View view) {
                            getActionBar().setTitle(mTitle);
                            invalidateOptionsMenu();

                    }

                    /** Called when a drawer is opened */
                    public void onDrawerOpened(View drawerView) {
                            getActionBar().setTitle("Androi Reboot");
                            invalidateOptionsMenu();
                    }

            };



            // Setting DrawerToggle on DrawerLayout
            mDrawerLayout.setDrawerListener(mDrawerToggle);

            // Creating an ArrayAdapter to add items to the listview mDrawerList
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), 
                            R.layout.arraylist, getResources().getStringArray(R.array.menus));

            // Setting the adapter on mDrawerList
            mDrawerList.setAdapter(adapter);

            // Enabling Home button
            getActionBar().setHomeButtonEnabled(true);

            // Enabling Up navigation
            getActionBar().setDisplayHomeAsUpEnabled(true);

            // Setting item click listener for the listview mDrawerList
            mDrawerList.setOnItemClickListener(new OnItemClickListener() {

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

                            // Getting an array of rivers
                            String[] menuItems = getResources().getStringArray(R.array.menus);

                            // Currently selected river
                            mTitle = menuItems[position];

                            // Creating a fragment object
                            MainActivity rFragment = new MainActivity();

                            // Passing selected item information to fragment
                            Bundle data = new Bundle();
                            data.putInt("position", position);
                            data.putString("url", getUrl(position));
                            rFragment.setArguments(data);


                            // Getting reference to the FragmentManager
                            FragmentManager fragmentManager = getFragmentManager();

                            // Creating a fragment transaction
                            FragmentTransaction ft = fragmentManager.beginTransaction();

                            // Adding a fragment to the fragment transaction
                            ft.replace(R.id.content_frame, rFragment);

                            // Committing the transaction
                            ft.commit();

                            // Closing the drawer
                            mDrawerLayout.closeDrawer(mDrawerList);

                    }
            });
    }

    protected String getUrl(int position) {
            switch (position) {
            case 0:
                    return "http://javatechig.com";
            default:
                    return "http://javatechig.com";
            }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            mDrawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                    return true;
            }
            return super.onOptionsItemSelected(item);
    }

    /** Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
            // If the drawer is open, hide action items related to the content view
            boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

            menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
            return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
    }
}

正如您所见,onClick会打开我的MainActivity(它是一个片段)。但我希望在我打开应用程序时显示它而不是在我点击时。感谢

1 个答案:

答案 0 :(得分:1)

试试这个

@Override
protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_listview);

        mTitle = "Androi Reboot";
        getActionBar().setTitle(mTitle);

        // Getting reference to the DrawerLayout
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawerList = (ListView) findViewById(R.id.drawer_list);

        // Getting reference to the ActionBarDrawerToggle
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                        R.drawable.ic_drawer, R.string.drawer_open,
                        R.string.drawer_close) {

                /** Called when drawer is closed */
                public void onDrawerClosed(View view) {
                        getActionBar().setTitle(mTitle);
                        invalidateOptionsMenu();

                }

                /** Called when a drawer is opened */
                public void onDrawerOpened(View drawerView) {
                        getActionBar().setTitle("Androi Reboot");
                        invalidateOptionsMenu();
                }

        };



        // Setting DrawerToggle on DrawerLayout
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        // Creating an ArrayAdapter to add items to the listview mDrawerList
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), 
                        R.layout.arraylist, getResources().getStringArray(R.array.menus));

        // Setting the adapter on mDrawerList
        mDrawerList.setAdapter(adapter);

        // Enabling Home button
        getActionBar().setHomeButtonEnabled(true);

        // Enabling Up navigation
        getActionBar().setDisplayHomeAsUpEnabled(true);

        // Setting item click listener for the listview mDrawerList
        mDrawerList.setOnItemClickListener(new OnItemClickListener() {

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

                        fireFragment(position)
                        // Closing the drawer
                        mDrawerLayout.closeDrawer(mDrawerList);

                }
        });

        fireFragment(0);
}

private void fireFragment(int position){
     // Getting an array of rivers
    String[] menuItems = getResources().getStringArray(R.array.menus);

    // Currently selected river
    mTitle = menuItems[position];

    // Creating a fragment object
    MainActivity rFragment = new MainActivity();

    // Passing selected item information to fragment
    Bundle data = new Bundle();
    data.putInt("position", position);
    data.putString("url", getUrl(position));
    rFragment.setArguments(data);


    // Getting reference to the FragmentManager
    FragmentManager fragmentManager = getFragmentManager();

    // Creating a fragment transaction
    FragmentTransaction ft = fragmentManager.beginTransaction();

    // Adding a fragment to the fragment transaction
    ft.replace(R.id.content_frame, rFragment);

    // Committing the transaction
    ft.commit();
}