android:导航抽屉点击问题

时间:2015-04-04 10:48:51

标签: android android-fragments navigation-drawer

我正在开发一个带有碎片抽屉布局的应用程序。 还有一个文本视图,说明了抽屉里的注销。

问题是注销最初效果很好。但是当我切换到不同的活动并返回到之前的活动然后打开抽屉时,注销变得无法点击,因为抽屉下面的内容被点击而不是注销。

抽屉布局的基本代码是

public class MainActivity extends ActionBarActivity {

private DrawerLayout mDrawerLayout;
private LinearLayout drawer_linear;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;

private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;    

public static final String TAG = "FeedTag";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    drawer_linear = (LinearLayout) findViewById(R.id.drawer_linear);

    findViewById(R.id.sign_out_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Do Something            
        }
    });

    mTitle = mDrawerTitle = getTitle();

    // load slide menu items
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    // nav drawer icons from resources
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    // adding nav drawer items to array
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1), true, String.valueOf(unreadCount)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));

    // Recycle the typed array
    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // setting the nav drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.drawable.ic_launcher);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            //R.drawable.ic_drawer, //nav menu toggle icon
            R.string.app_name, // nav drawer open - description for accessibility
            R.string.app_name // nav drawer close - description for accessibility
    ) {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu();
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        // on first time display view for first nav item
        displayView(0);
        mDrawerLayout.openDrawer(drawer_linear);
    }
    getSupportActionBar().setTitle(mDrawerTitle);

}

private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // display view for selected nav drawer item
        displayView(position);
    }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

/* *
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(drawer_linear);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);

    try {
        if(((Fragment)getSupportFragmentManager().findFragmentByTag("TimeTableFragment")).isVisible() && !drawerOpen) {
            menu.setGroupVisible(R.id.group_action_refresh, true);
            menu.findItem(R.id.action_remind).setVisible(true);
        }
    }
    catch (Exception e) {
    }

    return super.onPrepareOptionsMenu(menu);
}

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    String tag = "";
    switch (position) {
        case 0:
            fragment = new Fragment1();
            tag = "Fragment1";
            break;
        case 1:
            fragment = new Fragment2();
            tag = "Fragment2";
            break;
        case 2:
            fragment = new Fragment3();
            tag = "Fragment3";
            break;
        case 3:
            fragment = new Fragment4();
            tag = "Fragment4";
            break;
        case 4:
            fragment = new Fragment5();
            tag = "Fragment5";
            break;
        case 5:
            fragment = new Fragment6();
            tag = "Fragment6";
            break;
        case 6:
            fragment = new Fragment7();
            tag = "Fragment7";
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frame_container, fragment, tag);
        transaction.commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        //mDrawerLayout.closeDrawer(mDrawerList);
        mDrawerLayout.closeDrawer(drawer_linear);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

xml是

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v4.widget.DrawerLayout

    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->
    <LinearLayout
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/drawer_linear"
        android:layout_gravity="start"
        android:background="@color/list_background">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="150dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:id="@+id/userEmail"
                android:textColor="#ffffff"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"
                android:layout_marginBottom="5dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/userEmail"
                android:id="@+id/userName"
                android:textColor="#ffffff"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/abc_ic_clear_mtrl_alpha"
                android:id="@+id/sign_out_button"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"/>

        </RelativeLayout>

        <ListView
            android:id="@+id/list_slidermenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:listSelector="@drawable/list_selector"/>

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

0 个答案:

没有答案