android扩展我自己的活动

时间:2014-03-20 14:35:08

标签: android

我希望通过扩展mainactivity中的导航Activity,将菜单抽屉用于我的mainactivity。但我无法做到。这是我的导航Activity

 import android.app.Activity;
    import android.os.Build;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.TextView;

    import java.util.ArrayList;
    import java.util.List;

    import net.simonvt.menudrawer.MenuDrawer;
    import net.simonvt.menudrawer.Position;


    public class navigation extends Activity {

        private static final String STATE_ACTIVE_POSITION = "net.simonvt.menudrawer.samples.RightMenuSample.activePosition";
        private static final String STATE_CONTENT_TEXT = "net.simonvt.menudrawer.samples.RightMenuSample.contentText";

        private static final int MENU_OVERFLOW = 1;

        private MenuDrawer mMenuDrawer;

        private MenuAdapter mAdapter;
        private ListView mList;

        private int mActivePosition = -1;
        private String mContentText;
        private TextView mContentTextView;

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

            if (inState != null) {
                mActivePosition = inState.getInt(STATE_ACTIVE_POSITION);
                mContentText = inState.getString(STATE_CONTENT_TEXT);
            }

            mMenuDrawer = MenuDrawer.attach(this, MenuDrawer.MENU_DRAG_CONTENT, Position.RIGHT);
            mMenuDrawer.setContentView(R.layout.activity_rightmenu);

            List<Object> items = new ArrayList<Object>();
            items.add(new Item("Rap Battle", R.drawable.ic_action_select_all_dark));
            items.add(new Item("Nepali Songs", R.drawable.ic_action_select_all_dark));
            //items.add(new Category("Cat 1"));
            items.add(new Item("Nepali Short Movies", R.drawable.ic_action_select_all_dark));
            items.add(new Item("Newari Songs", R.drawable.ic_action_select_all_dark));
            //items.add(new Category("Cat 2"));
            items.add(new Item("Comedy Clips", R.drawable.ic_action_select_all_dark));
            items.add(new Item("Item Dance", R.drawable.ic_action_select_all_dark));
            items.add(new Item("Lok Dohori", R.drawable.ic_action_select_all_dark));
            items.add(new Item("Classical Songs", R.drawable.ic_action_select_all_dark));
            // A custom ListView is needed so the drawer can be notified when it's scrolled. This is to update the position
            // of the arrow indicator.
            mList = new ListView(this);
            mAdapter = new MenuAdapter(items);
            mList.setAdapter(mAdapter);
            mList.setOnItemClickListener(mItemClickListener);

            mMenuDrawer.setMenuView(mList);

            mContentTextView = (TextView) findViewById(R.id.contentText);
            mContentTextView.setText(mContentText);
        }

        private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mActivePosition = position;
                mMenuDrawer.setActiveView(view, position);
                mContentTextView.setText(((TextView) view).getText());
                mMenuDrawer.closeMenu();
            }
        };

        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putInt(STATE_ACTIVE_POSITION, mActivePosition);
            outState.putString(STATE_CONTENT_TEXT, mContentText);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuItem overflowItem = menu.add(0, MENU_OVERFLOW, 0, null);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                overflowItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
            }
            overflowItem.setIcon(R.drawable.ic_menu_moreoverflow_normal_holo_light);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case MENU_OVERFLOW:
                    mMenuDrawer.toggleMenu();
                    return true;
            }

            return super.onOptionsItemSelected(item);
        }

        @Override
        public void onBackPressed() {
            final int drawerState = mMenuDrawer.getDrawerState();
            if (drawerState == MenuDrawer.STATE_OPEN || drawerState == MenuDrawer.STATE_OPENING) {
                mMenuDrawer.closeMenu();
                return;
            }

            super.onBackPressed();
        }

        private static class Item {

            String mTitle;
            int mIconRes;

            Item(String title, int iconRes) {
                mTitle = title;
                mIconRes = iconRes;
            }
        }

        private static class Category {

            String mTitle;

            Category(String title) {
                mTitle = title;
            }
        }

        private class MenuAdapter extends BaseAdapter {

            private List<Object> mItems;

            MenuAdapter(List<Object> items) {
                mItems = items;
            }

            @Override
            public int getCount() {
                return mItems.size();
            }

            @Override
            public Object getItem(int position) {
                return mItems.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public int getItemViewType(int position) {
                return getItem(position) instanceof Item ? 0 : 1;
            }

            @Override
            public int getViewTypeCount() {
                return 2;
            }

            @Override
            public boolean isEnabled(int position) {
                return getItem(position) instanceof Item;
            }

            @Override
            public boolean areAllItemsEnabled() {
                return false;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                Object item = getItem(position);

                if (item instanceof Category) {
                    if (v == null) {
                        v = getLayoutInflater().inflate(R.layout.menu_row_category, parent, false);
                    }

                    ((TextView) v).setText(((Category) item).mTitle);

                } else {
                    if (v == null) {
                        v = getLayoutInflater().inflate(R.layout.menu_row_item, parent, false);
                    }

                    TextView tv = (TextView) v;
                    tv.setText(((Item) item).mTitle);
                    tv.setCompoundDrawablesWithIntrinsicBounds(((Item) item).mIconRes, 0, 0, 0);
                }

                v.setTag(R.id.mdActiveViewPosition, position);

                if (position == mActivePosition) {
                    mMenuDrawer.setActiveView(v, position);
                }

                return v;
            }
        }
    }

我的主要ActivityActivity中的启动器manifest。我认为扩展Activity是正确的!你能给我一些我出错的提示吗?

    package com.example.testmenu_drawer;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;

    public class Main1Activity extends navigation {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main1);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main1, menu);
            return true;
        }

    }

1 个答案:

答案 0 :(得分:0)

我不确定您的问题是什么,但请参阅此帖子以获取建议:android how to create my own Activity and extend it?

如果您创建一个摘要MainActivity,则有两个子类LauncherActivityOtherActivity

然后你将不必处理超类问题。