如何在视图寻呼机中启动片段2而不是片段1

时间:2014-03-19 18:01:55

标签: android android-fragments

您好我正在开发一个Android应用程序,它有一个视图寻呼机和三个片段默认情况下应用程序从片段1开始,但我希望它从片段2开始。我如何在android?

中执行此操作

继承我的viewpager代码:

    package com.learn2crack.tab;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;

public class TabPagerAdapter extends FragmentPagerAdapter {
    // Declare the number of ViewPager pages
        final int PAGE_COUNT = 3;

    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg) {
        switch (arg) {
        case 0:
            //Fragement for Calculator
            return new FragmentTab1();
        case 1:
           //Fragment for Calender
            return new FragmentTab2();
        case 2:
            //Fragment for Phone
            return new FragmentTab3();
        }
        return null;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return PAGE_COUNT;
    }


    }

如果你需要它,那就是我的主要活动:

package com.learn2crack.tab;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;


import com.d4a.tobias1.R;
import com.learn2crack.pager.Launchalot;

public class MainActivity extends FragmentActivity {
    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        getActionBar().hide();
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main);

        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());

        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {

                        actionBar = getActionBar();
                        actionBar.setSelectedNavigationItem(position);                    }
                });
        Tab.setAdapter(TabAdapter);

        actionBar = getActionBar();
        //Enable Tabs on Action Bar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){

            @Override
            public void onTabReselected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }

            @Override
             public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                Tab.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }};
            //Add New Tab
            actionBar.addTab(actionBar.newTab().setTabListener(tabListener));
            actionBar.addTab(actionBar.newTab().setTabListener(tabListener));
            actionBar.addTab(actionBar.newTab().setTabListener(tabListener));

    }
    /** Called when the user clicks the apps button */
    public void apps(View view) {
         Intent myIntent1 = new Intent(MainActivity.this, Launchalot.class);
    //   Intent myIntent1 = new Intent(MainActivity.this, Main.class);
        MainActivity.this.startActivity(myIntent1);
    }


    /** Called when the user clicks the notes button */
    public void notes(View view) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName("com.d4a.notes","com.example.note.NoteList"));
        intent.putExtra("grace", "Hi");
        startActivity(intent);


    }

    /** Called when the user clicks the play button */
    public void play(View view) {
        Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.vending");
        startActivity(launchIntent);
        }



    /** Called when the user clicks the web button */
    public void web(View view) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName("com.d4a.pegasus","acr.browser.barebones.activities.BrowserActivity"));
        intent.putExtra("grace", "Hi");
        startActivity(intent);

    }

       /** Called when the user clicks the email button */
 public void email(View view) {
      Intent intent = new Intent(Intent.ACTION_MAIN);
       intent.setComponent(new ComponentName("com.d4a.eMailTime","com.fsck.k9.activity.Accounts"));
       intent.putExtra("grace", "Hi");
       startActivity(intent);

 } 

 /** Called when the user clicks the sms button */
 public void chat(View view) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
     intent.setComponent(new ComponentName("com.d4a.sms","de.ub0r.android.smsdroid.ConversationListActivity"));
     intent.putExtra("grace", "Hi");
     startActivity(intent);


 }



}

任何帮助都会很棒

提前致谢!!

1 个答案:

答案 0 :(得分:3)

您是否尝试过调用tab.setCurrentItem(1);在onCreate()的末尾?