当我打开我的应用程序时,如何避免BottomBar自动选择AboutusTab?

时间:2016-07-15 11:03:38

标签: android android-fragments android-viewpager android-tablayout

当我打开我的应用程序时,默认选择Home_Tab打开,工作正常....但在这个Home_Tab,Bottombar也在那里,我不想要关于我们的默认选择标签? 如果我想点击AboutUs_Tab那个时候会打开??? 但在上面的图像中,当我打开Home_Tab,自动选择AboutUs_Tab但没有打开它时,会尝试点击那些关于us_Tab不工作??? 当我点击ContactUS_Tab并返回AboutUs_Tab时,那个时候AboutUS_Tab打开了??如何避免这个问题? enter image description here

这是我的代码

public class MainActivity extends FragmentActivity {

private Context context;
//This is our tablayout
private TabLayout tabLayout;
BottomBar bottombar;

//This is our viewPager
private ViewPager viewPager;
public ViewPagerAdapter FragmentStatePagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //setHasOptionsMenu(true);
    bottombar = BottomBar.attach(MainActivity.this, savedInstanceState);
    bottombar.noNavBarGoodness();
    bottombar.setItemsFromMenu(R.menu.bottom_bar_menu, new OnMenuTabSelectedListener() {
        @Override
        public void onMenuItemSelected(int itemId) {
            switch (itemId) {
                case R.id.about_us:
                    About_Us about_us = new About_Us();
                    About_Fragment about_fragment = new About_Fragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.Home_BottomBar_Fragment, about_fragment).commit();

                    break;
                case R.id.contact_us:
                    ContactUs_Activity contactUs_activity = new ContactUs_Activity();
                    Contact_us_Fragment contact_us_fragment = new Contact_us_Fragment();
                    FragmentManager fm2 = getSupportFragmentManager();
                    FragmentTransaction ft2 = fm2.beginTransaction();
                    ft2.replace(R.id.Home_BottomBar_Fragment, contact_us_fragment).commit();
                    //squareFragment.updateColor(Color.parseColor(colors[1]));
                    //Toast.makeText(MainActivity.this,"selected contact_us",Toast.LENGTH_SHORT).show();
                    break;

                case R.id.donate_us:
                    Donate_us_Fragment donate_us_fragment = new Donate_us_Fragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.Home_BottomBar_Fragment, donate_us_fragment).commit();
                    //Toast.makeText(MainActivity.this,"selected donate_us",Toast.LENGTH_SHORT).show();
                    break;
                default:
            }
        }
    });


    //Initializing the tablayout
    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    //Adding the tabs using addTab() method
    tabLayout.addTab(tabLayout.newTab().setText("Home").setIcon(R.drawable.home_selector));
    tabLayout.addTab(tabLayout.newTab().setText("News").setIcon(R.drawable.news_selector));
    tabLayout.addTab(tabLayout.newTab().setText("Videos").setIcon(R.drawable.video_selector));
    tabLayout.addTab(tabLayout.newTab().setText("PodCasts").setIcon(R.drawable.podcast_selector));
    tabLayout.addTab(tabLayout.newTab().setText("More").setIcon(R.drawable.more_selector));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    //tabLayout.setupWithViewPager(viewPager);

    //Initializing viewPager
    viewPager = (ViewPager) findViewById(R.id.pager);

    //Creating our pager adapter
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());

    //Adding adapter to pager
    viewPager.setAdapter(adapter);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            if (tab.getPosition() == 0) {
                bottombar.show();
                //bottombar.getCurrentTabPosition();
            } else if (tab.getPosition() == 1) {
                bottombar.hide();
            } else if (tab.getPosition() == 2) {
                bottombar.hide();
            } else if (tab.getPosition() == 3) {
                bottombar.hide();
            }else if (tab.getPosition() == 4){
                bottombar.hide();
            }
            super.onTabSelected(tab);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            super.onTabUnselected(tab);

            //tab.getIcon().setAlpha(127);
        }
    });
}

@Override
protected void onSaveInstanceState(Bundle outstate) {
    super.onSaveInstanceState(outstate);

    // Necessary to restore the BottomBar's state, otherwise we would
    // lose the current tab on orientation change.
    bottombar.onSaveInstanceState(outstate);
}

}

2 个答案:

答案 0 :(得分:0)

您可以使用以下方法手动设置底栏中的默认选定选项卡:

bottomBar.setDefaultTabPosition(1);

初始化底栏后使用此功能。

答案 1 :(得分:0)

您需要添加一个虚拟隐形项目,以使您的思考工作。为此,再创建一个项目:

<item android:id="@+id/none"
android:title=""/>

您创建商品的位置。然后在您的代码中更改为:

bottombar.setItemsFromMenu(R.menu.bottom_bar_menu, new OnMenuTabSelectedListener() {
        @Override
        public void onMenuItemSelected(int itemId) {
            switch (itemId) {
                case R.id.none:
                    break;
                case R.id.about_us:
                    About_Us about_us = new About_Us();
                    About_Fragment about_fragment = new About_Fragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.Home_BottomBar_Fragment, about_fragment).commit();

                    break;
                case R.id.contact_us:
                    ContactUs_Activity contactUs_activity = new ContactUs_Activity();
                    Contact_us_Fragment contact_us_fragment = new Contact_us_Fragment();
                    FragmentManager fm2 = getSupportFragmentManager();
                    FragmentTransaction ft2 = fm2.beginTransaction();
                    ft2.replace(R.id.Home_BottomBar_Fragment, contact_us_fragment).commit();
                    //squareFragment.updateColor(Color.parseColor(colors[1]));
                    //Toast.makeText(MainActivity.this,"selected contact_us",Toast.LENGTH_SHORT).show();
                    break;

                case R.id.donate_us:
                    Donate_us_Fragment donate_us_fragment = new Donate_us_Fragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.Home_BottomBar_Fragment, donate_us_fragment).commit();
                    //Toast.makeText(MainActivity.this,"selected donate_us",Toast.LENGTH_SHORT).show();
                    break;
                default:
            }
        }
    });