Android抽屉布局无需抽屉即可开始新活动

时间:2014-01-15 05:14:12

标签: android android-layout navigation-drawer drawer

我只是想使用布局开始以下活动。请帮助我并提前感谢。

我的xml和rest文件正在运行请直接浏览java文件并让我知道更改。出现NavigationDrawer但不响应所选项目。只显示相同的页面空白。

package com.example.myapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

// Within which the entire activity is enclosed`enter code here`
private DrawerLayout mDrawerLayout;

// ListView represents Navigation Drawer
private ListView mDrawerList;

// 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.activity_main);

    mTitle = "EDU LEARN";
    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("EDU LEARN");
            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.drawer_list_item, 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
            WebViewFragment rFragment = new WebViewFragment();

            // Passing selected item information to fragment
            Bundle data = new Bundle();
            data.putInt("position", position);
            // data.putString("url", string(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);

        }
    });
}

public void selectItem(int position) {
    Intent intent;
    switch (position) {
    case 0: {
        intent = new Intent(MainActivity.this, MainActivity1.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        break;
    }
    case 1: {
        Intent j = new Intent(MainActivity.this, login.class);
        startActivity(j);
        break;
    }
    case 2: {
        Intent a = new Intent(MainActivity.this, SplashScreen.class);
        startActivity(a);
        break;
    }
    case 3: {
        Intent i = new Intent(MainActivity.this, MainActivity1.class);
        startActivity(i);
        break;
    }
    case 4: {
        Intent i = new Intent(MainActivity.this, MainActivity1.class);
        startActivity(i);
        break;
    }
    case 5: {
        Intent i = new Intent(MainActivity.this, MainActivity1.class);
        startActivity(i);
        break;
    }
    case 6: {
        Intent i = new Intent(MainActivity.this, MainActivity1.class);
        startActivity(i);
        break;
    }
    default: {
        Intent i = new Intent(MainActivity.this, MainActivity.class);
        startActivity(i);
        break;
    }
    }

}

@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;
}

}

2 个答案:

答案 0 :(得分:3)

你有任何错误吗?每次你只是替换相同的片段!

你应该这样做:

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

       selectItem(position);

   }

答案 1 :(得分:0)

你不是在任何地方调用selectItem()方法。你的mDrawerList只是替换了fragment.So调用listview的onItemCLick的selectItem()方法。 在你的onItemClickListener中这样做:

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