簡體   English   中英

如何在視圖尋呼機中啟動片段2而不是片段1

[英]How to start on fragment 2 instead of fragment 1 in a view pager

您好我正在開發一個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);


 }



}

任何幫助都會很棒

提前致謝!!

你試過調用tab.setCurrentItem(1); 在onCreate()結尾?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM