我正在使用android studio提供的默認模板來創建帶有片段的選項卡式活動。每件事情都很好。我在其中有兩個選項卡,一個用於wifi,另一個用於移動網絡。我想要做的是在應用程序啓動時檢查用戶是否與WiFi或移動網絡連接。如果WiFi將他直接導航到WiFi選項卡,反之亦然。 我嘗試使用連接管理器來檢測網絡連接,但無法正確切換標籤頁。我沒有找到正確的地方,我應該怎麼改變標籤使用片段尋呼機適配器啓動時的默認片段
public class MainActivity extends AppCompatActivity {
TextView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
title= (TextView) findViewById(R.id.toolbar_title);
Fonts.setMonthoersFont(this,title);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FragmentWifi.newInstance(0);
case 1:
return FragmentMobileNetworks.newInstance(1);
default:
return new Fragment();
}
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
//
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Wi-Fi";
case 1:
return "Mobile Network";
}
return null;
}
}
}
發帖活動代碼 –