将导航栏扩展到另一个活动(没有片段)

时间:2015-10-09 18:11:23

标签: android navigation extends

我读到了一些关于此的问题,但所有的答案都是关于片段的,并且有类似于这个的问题,但答案是不完整的,我想将一组布局或代码重用到多个活动中,我创建了一个扩展的baseActivity使用以下代码进入活动。

我还读到你需要将代码放在onCreateOptionMenu中,但它仍然无效。 (baseacitivty xml中的代码正常工作,主页xml正在运行,但没有显示navigation_layout)

public class BaseActivity extends Activity {
    private ImageButton ibButtonHome;
    private ImageButton ibButtonFavorite;
    private ImageButton ibButtonRandomize;
    private ImageButton ibButtonHistory;
    private ImageButton ibButtonLogOut;

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

    }

    View.OnClickListener Navigation = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            if (v.equals(ibButtonHome)) {
                i.setClass(getBaseContext(), HomePage.class);
            } else if (v.equals(ibButtonFavorite)) {
                i.setClass(getBaseContext(), Favorite.class);
            } else if (v.equals(ibButtonHome)) {
                i.setClass(getBaseContext(), HomePage.class);
            } else if (v.equals(ibButtonRandomize)) {
                i.setClass(getBaseContext(), Randomize.class);
            } else if (v.equals(ibButtonHistory)) {
                i.setClass(getBaseContext(), History.class);
            } else if (v.equals(ibButtonLogOut)) {
                //TODO: something code here to not crash on activity exit??
                i.setClass(getBaseContext(), MainActivity.class);
            }

            startActivity(i);

        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu);
        ibButtonHome = (ImageButton) findViewById(R.id.button_Home);
        ibButtonFavorite = (ImageButton) findViewById(R.id.button_favorites);
        ibButtonRandomize = (ImageButton) findViewById(R.id.button_randomize);
        ibButtonHistory = (ImageButton) findViewById(R.id.button_history);
        ibButtonLogOut = (ImageButton) findViewById(R.id.button_logout);

        ibButtonFavorite.setOnClickListener(Navigation);
        ibButtonRandomize.setOnClickListener(Navigation);
        ibButtonHome.setOnClickListener(Navigation);
        ibButtonHistory.setOnClickListener(Navigation);
        ibButtonLogOut.setOnClickListener(Navigation);

        getMenuInflater().inflate(R.menu.menu_filter_menus, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

主页活动

    public class HomePage extends BaseActivity {
    private CustomAdpaterFoodFeed ExpAdapter;
    private ArrayList<FoodFeed> foodFeeds;
    private ExpandableListView ExpandList;
    //Onclick listener for the Navigation Bar

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

        ExpandList = (ExpandableListView) findViewById(R.id.evFoodFeed);



        //runs the function and returns the data to foodFeeds
        foodFeeds = SetStandardGroups();

        //Adapter for ExapadableListView
        ExpAdapter = new CustomAdpaterFoodFeed(HomePage.this, foodFeeds);
        ExpandList.setAdapter(ExpAdapter);



    }

    // Dummy data method for pictures and comments
    public ArrayList<FoodFeed> SetStandardGroups() {

        String names[] = {"Geraldine", "Marielle", "Gina", "Bryan",
                "Pat", "Eugene", "Shermaine", "Kook"};

        String comments[] = {"TasteGood", "Nah", "DONT EAT HERE", "Cameroon",
                "Nice place", "chill", "woah Spain", "lalala"};

        int Images[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                R.mipmap.ic_launcher, R.mipmap.ic_launcher
        };

        ArrayList<FoodFeed> list = new ArrayList<FoodFeed>();

        ArrayList<Comments> comments_list;


        for (int images : Images) {
            FoodFeed gru = new FoodFeed();
            gru.setIcon(images);

            comments_list = new ArrayList<Comments>();
            for (int j = 0; j < 4; j++) {
                Comments comments1 = new Comments();
                comments1.setName(names[j]);
                comments1.setComments(comments[j]);

                comments_list.add(comments1);
            }
            gru.setComments(comments_list);
            list.add(gru);


        }

        return list;
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

用于扩展导航抽屉的基本活动,您可以按照此链接进行其他活动,详细说明。它经过了很好的测试我遵循了相同的:))

http://androiddeveloperdemo.blogspot.in/2014/08/android-navigation-drawer-with-multiple.html

您可以在AndroidManifest.xml中声明这样的

,从而在其他活动中获得相同的操作栏
 <activity
        android:name=".SettingsActivity"
        android:label="@string/activity_title"
        android:theme="@style/AppTheme" />

对于不同的菜单选项,在android studio中的菜单文件夹下定义一个xml文件,然后将该文件膨胀 onCreateOptionsMenu(菜单)覆盖了您的激活方法