Android - DrawerMenu中的SubMenu

时间:2015-09-03 12:17:06

标签: java android

作为应用程序的一部分,我已经设置了一个menuDrawer。我想添加一个子菜单,但我不知道从哪里开始。这是我的代码:

MainActivity:

SELECT

}

NavigationDrawerFragement

public class MainActivity extends ActionBarActivity
    implements NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;
private Toolbar mToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(mToolbar);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.fragment_drawer);

    // Set up the drawer.
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);

}

@Override
public void onNavigationDrawerItemSelected(int position) {
    Fragment objFragment = null;

    switch (position) {
        case 0:
            objFragment = new menu1_Fragment();
            break;
        case 1:
            objFragment = new menu2_Fragment();
            break;
        case 2:
            objFragment = new menu3_Fragment();
            break;
        case 3:
            objFragment = new menu4_Fragment();
            break;
        case 4:
            objFragment = new menu5_Fragment();
            break;
        case 5:
            objFragment = new menu6_Fragment();
            break;
        case 6:
            objFragment = new menu8_Fragment();
            break;
        case 7:
            objFragment = new menu9_Fragment();
            break;
        case 8:
            objFragment = new menu10_Fragment();
            break;
        case 9:
            objFragment = new menu7_Fragment();
            break;
    }

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()

            .replace(R.id.container, objFragment)
            .commit();
}


@Override
public void onBackPressed() {
    if (mNavigationDrawerFragment.isDrawerOpen())
        mNavigationDrawerFragment.closeDrawer();
    else
        super.onBackPressed();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

activity_main:

public class NavigationDrawerFragment extends Fragment implements NavigationDrawerCallbacks {

/**
 * Remember the position of the selected item.
 */
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";

/**
 * Per the design guidelines, you should show the drawer on launch until the user manually
 * expands it. This shared preference tracks this.
 */
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";

/**
 * A pointer to the current callbacks instance (the Activity).
 */
private NavigationDrawerCallbacks mCallbacks;

/**
 * Helper component that ties the action bar to the navigation drawer.
 */
private ActionBarDrawerToggle mActionBarDrawerToggle;

private DrawerLayout mDrawerLayout;
private RecyclerView mDrawerList;
private View mFragmentContainerView;

private int mCurrentSelectedPosition = 0;
private boolean mFromSavedInstanceState;
private boolean mUserLearnedDrawer;

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

    // Read in the flag indicating whether or not the user has demonstrated awareness of the
    // drawer. See PREF_USER_LEARNED_DRAWER for details.
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
    mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    mDrawerList = (RecyclerView) view.findViewById(R.id.drawerList);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mDrawerList.setLayoutManager(layoutManager);
    mDrawerList.setHasFixedSize(true);

    final List<NavigationItem> navigationItems = getMenu();
    NavigationDrawerAdapter adapter = new NavigationDrawerAdapter(navigationItems);
    adapter.setNavigationDrawerCallbacks(this);
    mDrawerList.setAdapter(adapter);
    selectItem(mCurrentSelectedPosition);
    return view;
}

public boolean isDrawerOpen() {
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}

public ActionBarDrawerToggle getActionBarDrawerToggle() {
    return mActionBarDrawerToggle;
}

public DrawerLayout getDrawerLayout() {
    return mDrawerLayout;
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    selectItem(position);
}

public List<NavigationItem> getMenu() {
    List<NavigationItem> items = new ArrayList<NavigationItem>();
    items.add(new NavigationItem("1", getResources().getDrawable(R.drawable.1)));
    items.add(new NavigationItem("2", getResources().getDrawable(R.drawable.2)));
    items.add(new NavigationItem("3", getResources().getDrawable(R.drawable.3)));
    items.add(new NavigationItem("4", getResources().getDrawable(R.drawable.4)));
    items.add(new NavigationItem("5", getResources().getDrawable(R.drawable.5)));
    items.add(new NavigationItem("6", getResources().getDrawable(R.drawable.6)));
    items.add(new NavigationItem("7", getResources().getDrawable(R.drawable.7)));
    items.add(new NavigationItem("8", getResources().getDrawable(R.drawable.8)));
    items.add(new NavigationItem("9", getResources().getDrawable(R.drawable.9)));
    items.add(new NavigationItem("10", getResources().getDrawable(R.drawable.10)));
    return items;
}

/**
 * Users of this fragment must call this method to set up the navigation drawer interactions.
 *
 * @param fragmentId   The android:id of this fragment in its activity's layout.
 * @param drawerLayout The DrawerLayout containing this fragment's UI.
 * @param toolbar      The Toolbar of the activity.
 */
public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
    mFragmentContainerView = getActivity().findViewById(fragmentId);
    mDrawerLayout = drawerLayout;

    mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.myPrimaryDarkColor));

    mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (!isAdded()) return;

            getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if (!isAdded()) return;
            if (!mUserLearnedDrawer) {
                mUserLearnedDrawer = true;
                SharedPreferences sp = PreferenceManager
                        .getDefaultSharedPreferences(getActivity());
                sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
            }
            getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
        }
    };

    // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
    // per the navigation drawer design guidelines.
    if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
        mDrawerLayout.openDrawer(mFragmentContainerView);
    }

    // Defer code dependent on restoration of previous instance state.
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mActionBarDrawerToggle.syncState();
        }
    });

    mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
}

private void selectItem(int position) {
    mCurrentSelectedPosition = position;
    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerItemSelected(position);
    }
    ((NavigationDrawerAdapter) mDrawerList.getAdapter()).selectPosition(position);
}

public void openDrawer() {
    mDrawerLayout.openDrawer(mFragmentContainerView);
}

public void closeDrawer() {
    mDrawerLayout.closeDrawer(mFragmentContainerView);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mCallbacks = (NavigationDrawerCallbacks) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mCallbacks = null;
}

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

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Forward the new configuration the drawer toggle component.
    mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}

/*public void setUserData(String user, String email, Bitmap avatar) {
    ImageView avatarContainer = (ImageView) mFragmentContainerView.findViewById(R.id.imgAvatar);
    ((TextView) mFragmentContainerView.findViewById(R.id.txtUserEmail)).setText(email);
    ((TextView) mFragmentContainerView.findViewById(R.id.txtUsername)).setText(user);
    avatarContainer.setImageDrawable(new RoundImage(avatar));
}*/

public View getGoogleDrawer() {
    return mFragmentContainerView.findViewById(R.id.googleDrawer);
}

public static class RoundImage extends Drawable {
    private final Bitmap mBitmap;
    private final Paint mPaint;
    private final RectF mRectF;
    private final int mBitmapWidth;
    private final int mBitmapHeight;

    public RoundImage(Bitmap bitmap) {
        mBitmap = bitmap;
        mRectF = new RectF();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint.setShader(shader);

        mBitmapWidth = mBitmap.getWidth();
        mBitmapHeight = mBitmap.getHeight();
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawOval(mRectF, mPaint);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mRectF.set(bounds);
    }

    @Override
    public void setAlpha(int alpha) {
        if (mPaint.getAlpha() != alpha) {
            mPaint.setAlpha(alpha);
            invalidateSelf();
        }
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public int getIntrinsicWidth() {
        return mBitmapWidth;
    }

    @Override
    public int getIntrinsicHeight() {
        return mBitmapHeight;
    }

    public void setAntiAlias(boolean aa) {
        mPaint.setAntiAlias(aa);
        invalidateSelf();
    }

    @Override
    public void setFilterBitmap(boolean filter) {
        mPaint.setFilterBitmap(filter);
        invalidateSelf();
    }

    @Override
    public void setDither(boolean dither) {
        mPaint.setDither(dither);
        invalidateSelf();
    }

    public Bitmap getBitmap() {
        return mBitmap;
    }

}}

提前谢谢

0 个答案:

没有答案