如何在滑动菜单中添加一组新项目?

时间:2014-02-26 20:13:56

标签: android slidingmenu items

我想在我的滑动菜单中添加另一个块,或设置或其他任何项目,但我无法弄清楚要更改为第一个的代码...在我的滑动菜单中我有6项,但我有4个项目到第一个标题,然后,另一个标题,另外2个项目..但我无法弄清楚我有什么要改为我的第一个区块代码..非常感谢! !!!

这是我的MainActivity.java:

package com.orar.cngcnasaud;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity implements OnClickListener {

    private ListView mDrawerList;
    private DrawerLayout mDrawer;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private String[] menuItems;


    private static final String TAG = "AudioDemo";
    private static final String isPlaying = "Media is Playing"; 
    private static final String notPlaying = "Media has stopped Playing"; 

    MediaPlayer player;
    Button playerButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_drawer);
        player = MediaPlayer.create(this, R.raw.gc);

        player.setLooping(false); // Set looping

        // Get the button from the view
        playerButton = (Button) this.findViewById(R.id.buttonmp);
        playerButton.setText(R.string.play_label);
        playerButton.setTextColor(Color.WHITE);
        playerButton.setOnClickListener((OnClickListener) this);







        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        // set a custom shadow that overlays the main content when the drawer
        // opens
        mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        _initMenu();
        mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
        mDrawer.setDrawerListener(mDrawerToggle);
    }

        public void onClick(View v) {
            Log.d(TAG, "onClick: " + v);
            if (v.getId() == R.id.buttonmp) {
                playPause();
            }

    }




    private void demoPause() {
        // TODO Auto-generated method stub
        player.pause();
        playerButton.setText(R.string.play_label);

        Log.d(TAG, notPlaying);
}

    private void playPause() {
        // TODO Auto-generated method stub
        if(player.isPlaying()) {
          demoPause();
        } else {
          demoPlay();
        }   
    }

    private void demoPlay() {
        // TODO Auto-generated method stub
        player.start();
        playerButton.setText(R.string.stop_label);

        Log.d(TAG, isPlaying);
}




    private void _initMenu() {
        NsMenuAdapter mAdapter = new NsMenuAdapter(this);

        // Add Header
        mAdapter.addHeader(R.string.ns_menu_main_header);

        // Add first block

        menuItems = getResources().getStringArray(
                R.array.ns_menu_items);
        String[] menuItemsIcon = getResources().getStringArray(
                R.array.ns_menu_items_icon);

        int res = 0;
        for (String item : menuItems) {

            int id_title = getResources().getIdentifier(item, "string",
                    this.getPackageName());
            int id_icon = getResources().getIdentifier(menuItemsIcon[res],
                    "drawable", this.getPackageName());

            NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
            if (res==1) mItem.counter=0; //it is just an example...
            if (res==3) mItem.counter=0; //it is just an example...
            mAdapter.addItem(mItem);
            res++;
        }



        mDrawerList = (ListView) findViewById(R.id.drawer);
        if (mDrawerList != null)
            mDrawerList.setAdapter(mAdapter);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    }

    @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);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        /*
         * The action bar home/up should open or close the drawer.
         * ActionBarDrawerToggle will take care of this.
         */
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        // Handle your other action bar items...
        return super.onOptionsItemSelected(item);
    }

    private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle {

        public CustomActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout){
            super(
                mActivity,
                mDrawerLayout, 
                R.drawable.ic_drawer,
                R.string.ns_menu_open, 
                R.string.ns_menu_close);
        }

        @Override
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(getString(R.string.ns_menu_close));
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(getString(R.string.ns_menu_open));
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mDrawerList.setItemChecked(position, true);         
            mDrawer.closeDrawer(mDrawerList);
            if (position == 1) {
                Intent intent = new Intent(MainActivity.this, Istoric.class);
                startActivity(intent);
                mDrawer.closeDrawers();
            }
            else if (position == 2) {
                Intent intent2 = new Intent(MainActivity.this, Profesori.class);
                startActivity(intent2);
                mDrawer.closeDrawers();
            }
            if (position == 3) {
                Intent intent3 = new Intent(MainActivity.this, Elevi.class);
                startActivity(intent3);
                mDrawer.closeDrawers();
            }
            if (position == 4) {
                Intent intent4 = new Intent(MainActivity.this, ShowImageActivity.class);
                startActivity(intent4);
                mDrawer.closeDrawers();
            }

            if (position == 5) {
                Intent intent5 = new Intent(MainActivity.this, Despre.class);
                startActivity(intent5);
                mDrawer.closeDrawers();
            }
            if (position == 6) {
                Intent intent6 = new Intent(MainActivity.this, Feedback.class);
                startActivity(intent6);
                mDrawer.closeDrawers();
            }

        }



    }
        }

再次感谢!

1 个答案:

答案 0 :(得分:0)

我假设你想要实现类似于下图中的内容: enter image description here

您必须修改列表适配器(NsMenuAdapter)以支持两种类型的视图 - 项目和标题,其中项目是可点击的,标题只是一种群组标题。适配器应该采用对象的数组/列表,每个对象都应该具有type属性。这可能会使一点OnItemClickListener复杂化。

我发布了那种类型列表适配器的示例。在我的例子中,我创建了NavigationDrawerItem模型来保存有关项目类型,项目“可点击性”以及项目被点击后应该触发的操作的信息。

public class NavigationDrawerAdapter extends ArrayAdapter<NavigationDrawerItem> {
private static final String TAG = "NavigationDrawerAdapter";

public NavigationDrawerAdapter(Context context, int resource, NavigationDrawerItem[] objects) {
    super(context, resource, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (null == convertView) {
        int itemType = getItemViewType(position);
        if (NavigationDrawerItem.TYPE_TITLE == itemType) {
            convertView = View.inflate(getContext(), R.layout.item_title_navigation_drawer, null);
        } else if (NavigationDrawerItem.TYPE_ITEM_UNDER_TITLE == itemType) {
            convertView = View.inflate(getContext(), R.layout.item_under_title_navigation_drawer, null);
        } else {
            Log.e(TAG, "Returning default title view - Invalid drawer item type" + itemType);
            convertView = View.inflate(getContext(), R.layout.item_title_navigation_drawer, null);
        }
    }
    TextView tv = (TextView) convertView.findViewById(R.id.text);
    tv.setText(getContext().getString(getItem(position).stringRes));
    return convertView;
}

@Override
public int getItemViewType(int position) {
    return getItem(position).type;
}

@Override
public int getViewTypeCount() {
    return NavigationDrawerItem.NUMBER_OF_ITEM_TYPES;
}

@Override
public boolean isEnabled(int position) {
    return NavigationDrawerItem.isItemClickable(getItemViewType(position));
}

}

和NavigationDrawerItem类:

public final class NavigationDrawerItem {
private static final String TAG = "NavigationDrawerItem";

// Types must have numbers from 0 to n without gaps.
public static final int TYPE_TITLE = 0;// nonclickable
public static final int TYPE_ITEM_UNDER_TITLE = 1;// clickabel
public static final int NUMBER_OF_ITEM_TYPES = 2;

public final int action;
public final String argument;
public final int type;

    //Constructors etc.

public static final boolean isItemClickable(int itemType) {
    if (TYPE_TITLE == itemType) {
        return false;
    } else if (TYPE_ITEM_UNDER_TITLE == itemType) {
        return true;
    } else {
        Log.e(TAG, "Invalid item type " + itemType);
        return false;
    }
}

}