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

Android 将导航栏扩展到另一个活动(无片段),android,navigation,extends,Android,Navigation,Extends,我读了一些关于这方面的问题,但所有的答案都是关于片段的,有一个问题与此类似,但答案是不完整的,我想将一组布局或代码重用到多个活动中,我创建了一个baseActivity,它使用下面的代码扩展到活动中 我还了解到,您需要将代码放在onCreateOptionMenu中,但它仍然不起作用。(BaseActivity xml中的代码正常工作,主页xml正常工作,但不显示导航布局) 主页活动 public class HomePage extends BaseActivity { pri

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

我还了解到,您需要将代码放在onCreateOptionMenu中,但它仍然不起作用。(BaseActivity xml中的代码正常工作,主页xml正常工作,但不显示导航布局)

主页活动

    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);
    }
}
公共类主页扩展基本活动{
私有CustomAdpaterFoodFeed ExpAdapter;
私有ArrayList食物种子;
私有可扩展列表视图可扩展列表;
//单击导航栏的侦听器
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u主页);
ExpandList=(ExpandableListView)findViewById(R.id.evFoodFeed);
//运行函数并将数据返回到foodFeeds
foodFeeds=设置标准组();
//ExapadableListView的适配器
ExpAdapter=newcustomadpaterfoodfeed(HomePage.this,foodfeed);
ExpandList.setAdapter(ExpAdapter);
}
//图片和注释的虚拟数据方法
公共阵列列表集合标准组(){
字符串名称[]={“Geraldine”、“Marielle”、“Gina”、“Bryan”,
“帕特”、“尤金”、“谢尔曼”、“库克”};
字符串注释[]={“TasteGood”、“不”、“不要在这里吃”、“喀麦隆”,
“好地方”、“寒冷”、“西班牙哇”、“拉拉”};
int Images[]={R.mipmap.ic_启动器,R.mipmap.ic_启动器,
R.mipmap.ic_启动器,R.mipmap.ic_启动器,
R.mipmap.ic_启动器,R.mipmap.ic_启动器,
R.mipmap.ic_启动器,R.mipmap.ic_启动器
};
ArrayList=新建ArrayList();
ArrayList注释列表;
用于(整型图像:图像){
FoodFeed gru=新的FoodFeed();
gru.setIcon(图像);
注释列表=新的ArrayList();
对于(int j=0;j<4;j++){
Comments comments1=新注释();
注释1.集合名(名称[j]);
注释1.设置注释(注释[j]);
注释列表。添加(注释1);
}
gru.setComments(注释列表);
列表。添加(gru);
}
退货清单;
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单主页,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}

基本活动要在其他活动中扩展导航抽屉,您可以遵循此链接,详细介绍。它经过了很好的测试,我也遵循同样的原则:)

通过在AndroidManifest.xml中声明,您可以在其他活动中获得相同的操作栏,如下所示

 <activity
        android:name=".SettingsActivity"
        android:label="@string/activity_title"
        android:theme="@style/AppTheme" />

对于不同的菜单选项,在android studio中的菜单文件夹下定义一个xml文件,并在
一旦CreateOptionsMenu(菜单)覆盖了activity的方法,您就可以在其他活动中扩展导航抽屉了,如下所述。它经过了很好的测试,我也遵循同样的原则:)

通过在AndroidManifest.xml中声明,您可以在其他活动中获得相同的操作栏,如下所示

 <activity
        android:name=".SettingsActivity"
        android:label="@string/activity_title"
        android:theme="@style/AppTheme" />

对于不同的菜单选项,在android studio中的菜单文件夹下定义一个xml文件,并在
OnCreateOptions菜单(菜单)您活动的重写方法一次只能看到一个活动的布局,但您可以使用基本活动在其他活动中扩展导航抽屉您可以在片段中扩展活动的布局,因为它们是活动的一部分。我已将基本活动扩展到我的其他活动,但它仍然是活动的一部分不起作用,,我认为我缺少@Awadeshyes我还说过可以为导航抽屉和action barI使用基本活动在其他活动中扩展导航抽屉如果你愿意我可以发布整个代码一次只能看到一个活动的布局,但你可以使用基本活动在其他活动中扩展导航抽屉活动您可以在片段中扩展活动的布局,因为它们是活动的一部分。我已经将基本活动扩展到我的其他活动,但它仍然不起作用,我想我缺少一些@Awadeshyes我还说可以为导航抽屉和动作barI使用基本活动在其他活动中扩展导航抽屉如果你想我可以发布整个代码问题是我不知道如何删除侧栏中的动画,我只希望导航栏像instagram导航栏一样,我对安卓系统还是个新手:(问题是我不知道如何删除侧栏中的动画,我只希望导航栏像instagram导航栏一样,我对安卓系统还是个新手:(