[英]How can I execute only one tab of content in Android studio?
When I press tab 3, it will toast the message of tab 0, tab1, tab2, tab3. 当我按下选项卡3时,它将烘烤选项卡0,选项卡1,选项卡2,选项卡3的消息。 However, it displays the correct content of tab 3. How can I only execute the content of tab 3?
但是,它显示选项卡3的正确内容。如何仅执行选项卡3的内容? And why it will execute others pages?
为什么还要执行其他页面?
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount()));
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public SectionsPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
} @Override
public Fragment getItem(int position) {
position = mViewPager.getCurrentItem();
System.out.println("This is the currnet position : " + position);
if (position == 0) {
Tab0Index tab0 = new Tab0Index();
Toast.makeText(getApplicationContext(), "tab1 active " + position, Toast.LENGTH_SHORT).show();
return tab0;
} else if (position == 1) {
Tab2Top10 tab2 = new Tab2Top10();
Toast.makeText(getApplicationContext(), "tab2 active " + position, Toast.LENGTH_SHORT).show();
return tab2;
} else if (position == 2) {
Tab3UserRanking tab3 = new Tab3UserRanking();
Toast.makeText(getApplicationContext(), "tab3 active " + position, Toast.LENGTH_SHORT).show();
return tab3;
} else if (position == 3) {
Tab4Profile tab4 = new Tab4Profile();
Toast.makeText(getApplicationContext(), "tab4 active " + position, Toast.LENGTH_SHORT).show();
return tab4;
} else if (position == 4) {
Tab5Setting tab5 = new Tab5Setting();
Toast.makeText(getApplicationContext(), "tab5 active " + position, Toast.LENGTH_SHORT).show();
return tab5;
}else{
return null;
}
@Override
public int getCount() {
// Show 5 total pages.
return mNumOfTabs;
} }}
Don't use Toast.makeText(getApplicationContext(), "tab1 active " + position, Toast.LENGTH_SHORT).show();
不要使用
Toast.makeText(getApplicationContext(), "tab1 active " + position, Toast.LENGTH_SHORT).show();
in SectionsPagerAdapter
. 在
SectionsPagerAdapter
。 The view page already loads previous and next page when you select a page. 选择页面时,查看页面已加载上一页和下一页。 So change toast to onResume of each fragment(Pages).
因此,将Toast更改为每个片段(页面)的onResume。 for example.
例如。 for Tab 1 fragment's onResume(), use below code
对于Tab 1片段的onResume(),请使用以下代码
@Override
public void onResume() {
super.onResume();
if (getUserVisibleHint()) {
Toast.makeText(getApplicationContext(), "tab1 active " + position, Toast.LENGTH_SHORT).show();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.