android底栏设置创建活动时的默认选项卡

时间:2016-08-14 15:24:09

标签: android

我自己尝试使用底栏开发基于this tutorial的示例Android应用程序。

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thirdactivity);

        BottomBar bottomBar = BottomBar.attach(this, savedInstanceState);
        bottomBar.setItemsFromMenu(R.menu.bottom_menu, new OnMenuTabSelectedListener() {
            @Override
            public void onMenuItemSelected(int itemId) {
                 Intent myAct = new Intent();
            switch (itemId) {
                case R.id.item1:
                    myAct = new Intent(findViewById(itemId).getContext(), mainactivity.class);
                    break;
                case R.id.item2:
                    myAct = new Intent(findViewById(itemId).getContext(), secondactivity.class);
                    break;
                case R.id.item3:
                    myAct = new Intent(findViewById(itemId).getContext(), thirdactivity.class);
                    break;
            }
               startActivity(myAct);
            }
        });
    }

但是如何将第三个选项卡设置为默认值来创建活动。上面的代码突出显示了第一个选项卡,当单击第一个选项卡时甚至没有收听。此后,标签会打开相应的活动,但不会突出显示为当前标签。

编辑:我自己可以使用bottomBar.setDefaultTabPosition(desiredTabId);并且它可以工作,但它使用高内存。有什么问题?

2 个答案:

答案 0 :(得分:0)

通过将startActivity() as,

解决
 BottomBar bottomBar = BottomBar.attach(this, savedInstanceState);

      bottomBar.setItemsFromMenu(R.menu.bottom_menu, new OnMenuTabSelectedListener() {
                @Override
                public void onMenuItemSelected(int itemId) {
                    Intent act = new Intent();
                    if (R.id.item1 == itemId) {
                        act = new Intent(findViewById(itemId).getContext(), act1.class);
                        startActivity(act);
                        overridePendingTransition(R.anim.open_translate, R.anim.close_scale);
                    }
                    if (R.id.item2 == itemId) {
                        act = new Intent(findViewById(itemId).getContext(), act2.class);
                    }
                    if (R.id.item3 == itemId) {
                        act = new Intent(findViewById(itemId).getContext(), act3.class);
                        startActivity(act);
                        overridePendingTransition(R.anim.open_translate, R.anim.close_scale);
                    }

                }

            });
            bottomBar.setDefaultTabPosition(2);

            bottomBar.setActiveTabColor("#F3C030");

答案 1 :(得分:0)

在设置侦听器

之前尝试设置默认选项卡
bottomBar.setDefaultTab(R.id.tab_default);
bottomBar.setOnTabSelectListener(this);

这适用于版本2.0.2

如果在默认选项卡之前设置侦听器,则会调用它两次。 对于标签位置0,然后对于您设置为默认值的任何内容,通常不需要。

最好在创建底栏之前设置默认选项卡,或在自述文件中设置警告。