Example usage for android.support.v4.view ViewPager.SimpleOnPageChangeListener ViewPager.SimpleOnPageChangeListener

List of usage examples for android.support.v4.view ViewPager.SimpleOnPageChangeListener ViewPager.SimpleOnPageChangeListener

Introduction

In this page you can find the example usage for android.support.v4.view ViewPager.SimpleOnPageChangeListener ViewPager.SimpleOnPageChangeListener.

Prototype

ViewPager.SimpleOnPageChangeListener

Source Link

Usage

From source file:com.denel.facepatrol.MainActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mycontext = getApplicationContext();

    // Create the adapter that will return a fragment for each of the three primary sections
    // of the app.
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();

    // Specify that the Home/Up button should not be enabled, since there is no hierarchical
    // parent.//from   ww  w.  j  a va 2 s .com
    actionBar.setHomeButtonEnabled(false);

    // Specify that we will be displaying tabs in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager, attaching the adapter and setting up a listener for when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            // When swiping between different app sections, select the corresponding tab.
            // We can also use ActionBar.Tab#select() to do this if we have a reference to the
            // Tab.
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by the adapter.
        // Also specify this Activity object, which implements the TabListener interface, as the
        // listener for when this tab is selected.
        actionBar.addTab(
                actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
    }
}

From source file:app.screen.MyActivity.java

/**
 * For more info on using view pager and tabs and action bar, <a href="http://goo.gl/7CsM9">checkout this article</a>
 * in the Android developer site's "Training" section.
 * <p/>// www .jav a  2 s  .co m
 * For more info on ViewPager, <a href="http://goo.gl/NgCUO">read this article</a>.
 */
private void _setupFragments() {
    // set up the action bar
    final ActionBar actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // create the FragmentPagerAdapter
    FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            try {
                return Fragments.values()[position].aClass.getConstructor(MyActivity.class)
                        .newInstance(MyActivity.this);
            } catch (Exception e) {
                AndroidUtils.logErr(IconPaths.MyApp, "MyActivity - problem creating fragments", e);
                // something has gone wrong - this shouldn't happen
                return new Fragment() {
                    /** make a fragment to house {@link R.layout#error_fragment} */
                    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
                        return super.onCreateView(inflater, container, savedInstanceState);
                    }
                };
            }
        }

        @Override
        public int getCount() {
            return Fragments.values().length;
        }

        public CharSequence getPageTitle(int position) {
            try {
                return Fragments.values()[position].title;
            } catch (Exception e) {
                return "N/A";
            }
        }
    };

    // Set up the ViewPager, attaching the adapter and setting up a listener for when the
    // user swipes between sections.
    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setOffscreenPageLimit(Fragments.values().length); // keep all the pages in memory
    viewPager.setAdapter(fragmentPagerAdapter);
    viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        /**
         * When swiping between different app sections, select the corresponding tab.
         * We can also use ActionBar.Tab#select() to do this if we have a reference to the Tab.
         */
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            actionBar.setSelectedNavigationItem(position);
        }
    });
    // create TabListener
    ActionBar.TabListener tabListener = new

    ActionBar.TabListener() {
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction transaction) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction transaction) {
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction transaction) {
        }
    };

    // setup tabs
    for (int i = 0; i < fragmentPagerAdapter.getCount(); i++) {
        actionBar.addTab(
                actionBar.newTab().setText(fragmentPagerAdapter.getPageTitle(i)).setTabListener(tabListener));
    }

}