使用 Fragment Pager Adapter 启动时的默认片段

Default fragment on startup with Fragment Pager Adapter

我正在使用 android studio 提供的默认模板创建带片段的选项卡 activity。一切正常 fine.I 有 2 个选项卡,一个用于 wifi,另一个用于移动网络。我想要做的是在应用程序启动时检查用户是否连接到 wifi 或移动 network.If wifi 将他直接导航到 wifi 选项卡,反之亦然。 我尝试使用连接管理器来检测 netwotk 连接,但无法正确切换选项卡。我没有找到正确的位置以及如何更改标签页

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;
    }
}

}

试试这个

    ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);


    final ConnectivityManager connMgr = (ConnectivityManager)
    this.getSystemService(Context.CONNECTIVITY_SERVICE);
    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifi.isConnectedOrConnecting ()) {
        mViewPager.setCurrentItem(0);
    } else if (mobile.isConnectedOrConnecting ()) {
        mViewPager.setCurrentItem(1);
    } else {
        Toast.makeText(this, "No Network ", Toast.LENGTH_LONG).show();
    }

在您的 Activity/Fragment 中,一旦 PagerAdapter 连接到寻呼机,(确保 activity 的 onResume 已被调用,检查网络状态并调用它):

if(isConnectedWithWifi){
mViewPager.setCurrentItem(_POSITION_OF_FRAGMENT_WH);
}