Example usage for android.support.v4.app FragmentPagerAdapter getCount

List of usage examples for android.support.v4.app FragmentPagerAdapter getCount

Introduction

In this page you can find the example usage for android.support.v4.app FragmentPagerAdapter getCount.

Prototype

public abstract int getCount();

Source Link

Document

Return the number of views available.

Usage

From source file:com.karumi.dividers.sample.SampleActivity.java

private void initializeViewPager() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentPagerAdapter pagerAdapter = new SampleFragmentPagerAdapter(fragmentManager, this);
    viewPager.setAdapter(pagerAdapter);//  ww  w .  j  a  v a  2  s . c o  m
    viewPager.setOffscreenPageLimit(pagerAdapter.getCount());
}

From source file:com.cereuswomen.marketingmessage.view.SlidingTabLayout.java

public void populateTabStrip(int position) {

    final FragmentPagerAdapter adapter = (FragmentPagerAdapter) mViewPager.getAdapter();
    final View.OnClickListener tabClickListener = new TabClickListener();

    for (int i = position; i < adapter.getCount(); i++) {
        View tabView = null;/*w w w.j a  v a  2  s  .  c o m*/
        TextView tabTitleView = null;

        if (mTabViewLayoutId != 0) {
            // If there is a custom tab view layout id set, try and inflate it
            tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
            tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
        }

        if (tabView == null) {
            tabView = createDefaultTabView(getContext());
        }

        if (tabTitleView == null && TextView.class.isInstance(tabView)) {
            tabTitleView = (TextView) tabView;
        }

        tabTitleView.setText(adapter.getPageTitle(i));
        tabView.setOnClickListener(tabClickListener);

        mTabStrip.addView(tabView);
    }

}

From source file:com.example.android.OurApp.MainActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);/*from w ww.  j  a  va2  s.  co  m*/

    // 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.
    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);
    FragmentPagerAdapter pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        // ?position?Fragment
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new DummyFragment();
            Bundle args = new Bundle();
            args.putInt(DummyFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

        // iAdapterFragment
        @Override
        public int getCount() {
            return 3;
        }

        // ?Fragment
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return "";
            case 1:
                return "";
            case 2:
                return "";
            }
            return null;
        }
    };

    for (int i = 0; i < pagerAdapter.getCount(); i++) {

        actionBar.addTab(actionBar.newTab().setText(pagerAdapter.getPageTitle(i)).setTabListener(this));
    }

    mViewPager.setAdapter(pagerAdapter);
    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);
        }
    });

}

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/>//from w w  w . j  a  v 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));
    }

}