Intent to open a specific tab of tabbed activity

断了今生、忘了曾经 提交于 2019-12-21 23:33:56

问题


I have a tabbed activity with 5 tabs. Each tab has only one Imageview. On a previous page I have 5 buttons and I want to create an interface such that each button starts the tabbed activity but the first tab which is visible is specific to that button. eg. gallery apps open a specific tab corresponding to the thumnail of the photo and are also left/right swappable.


回答1:


You can pass the tab id you want to open as an extra to the Intent you are creating. Then in the tabbed Activity, assuming you are using TabLayout, you can do something like this -

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
TabLayout.Tab tab = tabLayout.getTabAt(getIntent().getStringExtra("selected_index"));
tab.select();



回答2:


Try This

  1. First activity

     int page = 2;
     Intent intent = new Intent(FirstActivity.this,TabActivityClass.class);
     intent.putExtra("One", page);// One is your argument 
     startActivity(intent);
    

    2.In oncreate method of TabActivity class

    int defaultValue = 0;
    int page = getIntent().getIntExtra("One", defaultValue);
    viewPager.setCurrentItem(page);
    


来源:https://stackoverflow.com/questions/36583719/intent-to-open-a-specific-tab-of-tabbed-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!