导航抽屉设计支持

时间:2015-06-15 21:51:10

标签: android navigation-drawer nav

导航抽屉没有响应点击和零食也没有显示出来。 如何使点击响应并添加片段以进行数据加载。 它们是抽屉中的两个项目,只有一个在选择时进行检查。

package com.example.jasaran.nav;

import com.example.jasaran.nav.R;
import android.content.Context;    
import android.content.SharedPreferences;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;


public class MainActivity extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener {

private Toolbar mToolbar;
private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView;
private FrameLayout mContentFrame;
private int mCurrentSelectedPosition;
private static final String PREFERENCES_FILE = "nav_settings";
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
private boolean mUserLearnedDrawer;
int mselected;

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

    setContentView(R.layout.activity_main);

    setUpToolbar();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer);

    mUserLearnedDrawer = Boolean.valueOf(readSharedSetting(this, PREF_USER_LEARNED_DRAWER, "false"));

    if (savedInstanceState != null) {
        mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
        boolean mFromSavedInstanceState = true;
    }

    setUpNavDrawer();

    mNavigationView = (NavigationView) findViewById(R.id.nav_view);
    mContentFrame = (FrameLayout) findViewById(R.id.nav_contentframe);

    mNavigationView.setNavigationItemSelectedListener(this);

    }


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION, 0);
    Menu menu = mNavigationView.getMenu();
    menu.getItem(mCurrentSelectedPosition).setChecked(true);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
    }
    return super.onOptionsItemSelected(item);
}

private void setUpToolbar() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    if (mToolbar != null) {
        setSupportActionBar(mToolbar);
    }
}

private void setUpNavDrawer() {
    if (mToolbar != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationIcon(R.drawable.ic_launcher);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(GravityCompat.START);
            }
        });
    }

    if (!mUserLearnedDrawer) {
        mDrawerLayout.openDrawer(GravityCompat.START);
        mUserLearnedDrawer = true;
        saveSharedSetting(this, PREF_USER_LEARNED_DRAWER, "true");
    }

}

public static void saveSharedSetting(Context ctx, String settingName, String settingValue) {
    SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(settingName, settingValue);
    editor.apply();
}

public static String readSharedSetting(Context ctx, String settingName, String defaultValue) {
    SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
    return sharedPref.getString(settingName, defaultValue);
}

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
    menuItem.setChecked(true);
   mselected = menuItem.getItemId();
    switch (menuItem.getItemId()) {

        case R.id.navigation_item_1:
            Snackbar.make(mContentFrame, "Item One", Snackbar.LENGTH_SHORT).show();
            mCurrentSelectedPosition = 0;
            return true;

        case R.id.navigation_item_2:
            Snackbar.make(mContentFrame, "Item Two", Snackbar.LENGTH_SHORT).show();
            mCurrentSelectedPosition = 1;
            return true;

        default:
            return true;
    }
}
}

1 个答案:

答案 0 :(得分:1)

我看到你直接从Activity实现了监听器。尝试将它附加到NavigationView,如下所示:

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            // Handle menu clicks here

        }
    });

GitHub上查看此活动代码。