Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ViewPager设置不同的操作栏内容&;航行_Android_Android Fragments_Android Actionbar_Android Menu_Up Navigation - Fatal编程技术网

Android ViewPager设置不同的操作栏内容&;航行

Android ViewPager设置不同的操作栏内容&;航行,android,android-fragments,android-actionbar,android-menu,up-navigation,Android,Android Fragments,Android Actionbar,Android Menu,Up Navigation,使用ViewPager,我有两个片段:“主”和“第二个”。 1) 我想为这两个片段设置不同的操作栏标题。 2) 我想为这两个片段设置不同的操作栏项。 3) 当我处于“秒”位置时,我想执行到“主”的“向上导航”。 My MainActivity.java: public class MainActivity extends ActionBarActivity { /** * The {@link android.support.v4.view.PagerAdapter} th

使用ViewPager,我有两个片段:“主”和“第二个”。
1) 我想为这两个片段设置不同的操作栏标题。
2) 我想为这两个片段设置不同的操作栏项。
3) 当我处于“秒”位置时,我想执行到“主”的“向上导航”。

My MainActivity.java:

public class MainActivity extends ActionBarActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
     * derivative, which will keep every loaded fragment in memory. If this
     * becomes too memory intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 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) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class
            // below).
            switch (position) {
            case 0:
                return new FragmentMain();
            case 1:
                return new FragmentSettings();
            }
            return null;
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }
    }
}
您可以向ViewPager中添加一个。在
onPageSelected
回调方法中进行调整。例如

@Override
public void onPageSelected(int position) {
    // set Activity title
    setTitle(/*get title based on position*/);
}