如何在android中显示默认选项卡?

时间:2016-06-21 11:35:56

标签: android

我有一个添加tablayout的活动,即" A"," B"当我搬到" B"并退出该活动并再次恢复到该活动标签" B"对我来说是可见的。但我想每当返回该活动时总是显示标签" A"。我怎么能这样做

代码: -

    public void init() {
    s_oCloginSession = new CLoginSessionManagement(CMainActivity.this);// object creation of Login Session...
    setupToolbar();// setting toolbar

    // navigation bar code
    m_Drawer = (DrawerLayout) findViewById(R.id.drawer_layout);//finding id of drawerlayout
    s_drawerToggle = new ActionBarDrawerToggle(
            this, m_Drawer, m_Toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    m_Drawer.setDrawerListener(s_drawerToggle);
    m_Drawer.setScrimColor(getResources().getColor(android.R.color.transparent));
    s_drawerToggle.syncState();

    NavigationView m_Navigation = (NavigationView) findViewById(R.id.nav_view);
    m_Navigation.setNavigationItemSelectedListener(this);

    m_TabLayout = (TabLayout) findViewById(R.id.tab_layout);// finding Id of tablayout
    m_ViewPager = (ViewPager) findViewById(R.id.pager);//finding Id of ViewPager
    m_TabLayout.addTab(m_TabLayout.newTab().setText("Deals"));// add deal listin tab
    m_TabLayout.addTab(m_TabLayout.newTab().setText("Stories"));// add stories tab
    m_TabLayout.setTabGravity(TabLayout.GRAVITY_FILL);// setting Gravity of Tab


    CDealMainListingPager m_oDealMainScreenPager = new CDealMainListingPager(getSupportFragmentManager(), m_TabLayout.getTabCount());
    m_ViewPager.setAdapter(m_oDealMainScreenPager);// adiing adapter to ViewPager
    m_ViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(m_TabLayout));// performing action of page changing
    m_TabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            m_ViewPager.setCurrentItem(tab.getPosition());
        }

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

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

}

1 个答案:

答案 0 :(得分:1)

我的朋友m_ViewPager.setCurrentItem(0);将以onResume方法为您提供帮助:

@Override
protected void onResume() {
    if(m_ViewPager!=null){
        m_ViewPager.setCurrentItem(0);
    }
}