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

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

Introduction

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

Prototype

public ViewPager(Context context) 

Source Link

Usage

From source file:fr.android.earthdawn.activities.CharacterSheetActivity.java

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);/* w w w .ja  v  a  2 s  .com*/
    setContentView(mViewPager);

    final ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    mTabsAdapter = new TabsAdapter(this, mViewPager);

    // Load character and store it in bundle
    final EDCharacter character = CharacterManager.getLoadedCharacter();
    Bundle bundle;

    // Infos gnrales
    mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_description), CharacterFragment.class, null);
    // Talents, par discipline
    if (character.getMainDiscipline() != null) {
        bundle = new Bundle(1);
        bundle.putSerializable(Constants.BUNDLE_DISCIPLINE, character.getMainDiscipline());
        mTabsAdapter.addTab(bar.newTab().setText(character.getMainDiscipline().getName()),
                TalentsFragment.class, bundle);
    }
    if (character.getSecondDiscipline() != null) {
        bundle = new Bundle(1);
        bundle.putSerializable(Constants.BUNDLE_DISCIPLINE, character.getSecondDiscipline());
        mTabsAdapter.addTab(bar.newTab().setText(character.getSecondDiscipline().getName()),
                TalentsFragment.class, bundle);
    }
    if (character.getThirdDiscipline() != null) {
        bundle = new Bundle(1);
        bundle.putSerializable(Constants.BUNDLE_DISCIPLINE, character.getThirdDiscipline());
        mTabsAdapter.addTab(bar.newTab().setText(character.getThirdDiscipline().getName()),
                TalentsFragment.class, bundle);
    }
    // Equipment
    mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_stuff), EquipmentFragment.class, null);

    // Skill
    mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_skills), SkillsFragment.class, null);

    // TODO Grimoire

    if (savedInstanceState != null) {
        bar.setSelectedNavigationItem(savedInstanceState.getInt(TAB, 0));
    }
}

From source file:com.cyanogenmod.settings.device.DeviceSettings.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.viewPager);//from w w  w.j  a  v  a2  s.  co  m
    setContentView(mViewPager);

    final ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
    bar.setTitle(R.string.app_name);

    mTabsAdapter = new TabsAdapter(this, mViewPager);
    mTabsAdapter.addTab(bar.newTab().setText(R.string.category_screen_title), ScreenFragmentActivity.class,
            null);
    mTabsAdapter.addTab(bar.newTab().setText(R.string.category_sensors_title), SensorsFragmentActivity.class,
            null);
    mTabsAdapter.addTab(bar.newTab().setText(R.string.category_haptic_title), HapticFragmentActivity.class,
            null);
    mTabsAdapter.addTab(bar.newTab().setText(R.string.category_storage_title), StorageFragmentActivity.class,
            null);
    mTabsAdapter.addTab(bar.newTab().setText(R.string.category_dock_title), DockFragmentActivity.class, null);

    if (savedInstanceState != null) {
        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }
}

From source file:org.dpadgett.timer.TimerActivity.java

/** Called when the activity is first created. */
@Override/*w w w  .  j  a  v a2 s .  com*/
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ViewPager mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.viewPager);
    mViewPager.setOffscreenPageLimit(Tab.values().length);
    setContentView(mViewPager);

    final ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
    bar.setDisplayShowHomeEnabled(false);

    final Bundle extras = getIntent().getExtras();

    mTabsAdapter = new TabsAdapter(this, mViewPager);
    final Context context = getApplicationContext();
    for (final Tab tab : Tab.values()) {
        final String tabTitle = context.getString(tab.getTitle());
        mTabsAdapter.addTab(bar.newTab().setText(tabTitle), tab.getFragmentClass(), extras);
    }

    StartReason startReason = StartReason.START_REASON_NONE;
    if (extras != null) {
        startReason = (StartReason) extras.getSerializable(START_REASON);
    }

    final SharedPreferences prefs = getSharedPreferences("TimerActivity", Context.MODE_PRIVATE);
    if (startReason == StartReason.START_REASON_AUTOSTART_STOPWATCH) {
        bar.setSelectedNavigationItem(Tab.STOPWATCH.ordinal());
    } else if (prefs.contains("tab")) {
        bar.setSelectedNavigationItem(prefs.getInt("tab", 0));
    } else if (savedInstanceState != null) {
        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }

    alarmDialog = new AlertDialog.Builder(this).setTitle(context.getString(R.string.countdown_timer_finished))
            .setPositiveButton(context.getString(R.string.alarm_dialog_dismiss),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(final DialogInterface dialog, final int which) {
                            dialog.dismiss();
                            final Intent intent = new Intent(TimerActivity.this, AlarmService.class)
                                    .putExtra("startAlarm", false).putExtra("fromFragment", true)
                                    .setAction("stopAlarm");
                            TimerActivity.this.startService(intent);
                        }
                    })
            .setCancelable(false).create();

    if (prefs.getBoolean("countdownDialogShowing", false)) {
        alarmDialog.show();
    }

    getApplicationContext().registerReceiver(showDialogReceiver, new IntentFilter(ACTION_SHOW_DIALOG));
    getApplicationContext().registerReceiver(dismissDialogReceiver, new IntentFilter(ACTION_DISMISS_DIALOG));
}

From source file:org.adaway.ui.HelpActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);//w ww .j a  va2s .com

    setContentView(mViewPager);
    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowTitleEnabled(true);
    bar.setDisplayHomeAsUpEnabled(true);

    mTabsAdapter = new TabsAdapter(this, mViewPager);

    Bundle faqBundle = new Bundle();
    faqBundle.putInt(HelpFragmentHtml.ARG_HTML_FILE, R.raw.help_faq);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.help_tab_faq)), HelpFragmentHtml.class,
            faqBundle);

    Bundle problemsBundle = new Bundle();
    problemsBundle.putInt(HelpFragmentHtml.ARG_HTML_FILE, R.raw.help_problems);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.help_tab_problems)), HelpFragmentHtml.class,
            problemsBundle);

    Bundle sOnSOffBundle = new Bundle();
    sOnSOffBundle.putInt(HelpFragmentHtml.ARG_HTML_FILE, R.raw.help_s_on_s_off);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.help_tab_s_on_s_off)), HelpFragmentHtml.class,
            sOnSOffBundle);

    Bundle donationsArgs = new Bundle();
    donationsArgs.putBoolean(DonationsFragment.ARG_DEBUG, Constants.DEBUG);
    donationsArgs.putBoolean(DonationsFragment.ARG_FLATTR_ENABLED, true);
    donationsArgs.putString(DonationsFragment.ARG_FLATTR_PROJECT_URL, FLATTR_PROJECT_URL);
    donationsArgs.putString(DonationsFragment.ARG_FLATTR_URL, FLATTR_URL);

    donationsArgs.putBoolean(DonationsFragment.ARG_PAYPAL_ENABLED, true);
    donationsArgs.putString(DonationsFragment.ARG_PAYPAL_CURRENCY_CODE, PAYPAL_CURRENCY_CODE);
    donationsArgs.putString(DonationsFragment.ARG_PAYPAL_USER, PAYPAL_USER);
    donationsArgs.putString(DonationsFragment.ARG_PAYPAL_ITEM_NAME,
            getString(R.string.help_donation_paypal_item));

    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.help_tab_donate)), DonationsFragment.class,
            donationsArgs);

    Bundle changelogArgs = new Bundle();
    changelogArgs.putInt(HelpFragmentHtml.ARG_HTML_FILE, R.raw.help_changelog);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.help_tab_changelog)), HelpFragmentHtml.class,
            changelogArgs);

    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.help_tab_about)), HelpFragmentAbout.class,
            null);
}

From source file:com.brq.wallet.activity.modern.GetFromAddressBookActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MbwManager _mbwManager = MbwManager.getInstance(this);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);// ww w .j a v  a2s  .  c  om

    setContentView(mViewPager);

    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mTabsAdapter = new TabsAdapter(this, mViewPager, _mbwManager);

    Tab myAddressesTab = bar.newTab();
    mTabsAdapter.addTab(myAddressesTab.setText(getResources().getString(R.string.my_accounts)),
            AddressBookFragment.class, addressBookBundle(true));
    Tab contactsTab = bar.newTab();
    mTabsAdapter.addTab(contactsTab.setText(getResources().getString(R.string.foreign_addresses)),
            AddressBookFragment.class, addressBookBundle(false));

    int countContactsEntries = _mbwManager.getMetadataStorage().getAllAddressLabels().size();

    if (countContactsEntries > 0) {
        bar.selectTab(contactsTab);
    } else {
        bar.selectTab(myAddressesTab);
    }
}

From source file:ti.modules.titanium.ui.widget.TiUIScrollableView.java

private ViewPager buildViewPager(Context context, ViewPagerAdapter adapter) {
    ViewPager pager = (new ViewPager(context) {
        @Override//from w  w  w. j a va 2 s  .  com
        public boolean onTouchEvent(MotionEvent event) {
            if (mEnabled) {
                return super.onTouchEvent(event);
            }

            return false;
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent event) {
            if (mEnabled) {
                return super.onInterceptTouchEvent(event);
            }

            return false;
        }
    });

    pager.setAdapter(adapter);
    pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        private boolean isValidScroll = false;
        private boolean justFiredDragEnd = false;

        @Override
        public void onPageScrollStateChanged(int scrollState) {
            if ((scrollState == ViewPager.SCROLL_STATE_IDLE) && isValidScroll) {
                int oldIndex = mCurIndex;

                if (mCurIndex >= 0) {
                    if (oldIndex >= 0 && oldIndex != mCurIndex && oldIndex < mViews.size()) {
                        // Don't know what these focused and unfocused
                        // events are good for, but they were in our previous
                        // scrollable implementation.
                        // cf. https://github.com/appcelerator/titanium_mobile/blob/20335d8603e2708b59a18bafbb91b7292278de8e/android/modules/ui/src/ti/modules/titanium/ui/widget/TiScrollableView.java#L260
                        TiEventHelper.fireFocused(mViews.get(oldIndex));
                    }

                    TiEventHelper.fireUnfocused(mViews.get(mCurIndex));
                    if (oldIndex >= 0) {
                        // oldIndex will be -1 if the view has just
                        // been created and is setting currentPage
                        // to something other than 0. In that case we
                        // don't want a `scrollend` to fire.
                        ((ScrollableViewProxy) proxy).fireScrollEnd(mCurIndex, mViews.get(mCurIndex));
                    }

                    if (shouldShowPager()) {
                        showPager();
                    }
                }

                // If we don't use this state variable to check if it's a valid
                // scroll, this event will fire when the view is first created
                // because on creation, the scroll state is initialized to 
                // `idle` and this handler is called.
                isValidScroll = false;
            } else if (scrollState == ViewPager.SCROLL_STATE_SETTLING) {
                ((ScrollableViewProxy) proxy).fireDragEnd(mCurIndex, mViews.get(mCurIndex));

                // Note that we just fired a `dragend` so the `onPageSelected`
                // handler below doesn't fire a `scrollend`.  Read below comment.
                justFiredDragEnd = true;
            }
        }

        @Override
        public void onPageSelected(int page) {

            // If we didn't just fire a `dragend` event then this is the case
            // where a user drags the view and settles it on a different view.
            // Since the OS settling logic is never run, the
            // `onPageScrollStateChanged` handler is never run, and therefore
            // we forgot to inform the Javascripters that the user just scrolled
            // their thing.

            if (!justFiredDragEnd && mCurIndex != -1) {
                ((ScrollableViewProxy) proxy).fireScrollEnd(mCurIndex, mViews.get(mCurIndex));

                if (shouldShowPager()) {
                    showPager();
                }
            }
        }

        @Override
        public void onPageScrolled(int positionRoundedDown, float positionOffset, int positionOffsetPixels) {
            isValidScroll = true;

            // When we touch and drag the view and hold it inbetween the second
            // and third sub-view, this function will have been called with values
            // similar to:
            //      positionRoundedDown:   1
            //      positionOffset:          0.5
            // ie, the first parameter is always rounded down; the second parameter
            // is always just an offset between the current and next view, it does
            // not take into account the current view.

            // If we add positionRoundedDown to positionOffset, positionOffset will
            // have the 'correct' value; ie, will be a natural number when we're on
            // one particular view, something.5 when inbetween views, etc.
            float positionFloat = positionOffset + positionRoundedDown;

            // `positionFloat` can now be used to calculate the correct value for
            // the current index. We add 0.5 so that positionFloat will be rounded
            // half up; ie, if it has a value of 1.5, it will be rounded up to 2; if
            // it has a value of 1.4, it will be rounded down to 1.
            mCurIndex = (int) Math.floor(positionFloat + 0.5);
            ((ScrollableViewProxy) proxy).fireScroll(mCurIndex, positionFloat, mViews.get(mCurIndex));

            // Note that we didn't just fire a `dragend`.  See the above comment
            // in `onPageSelected`.
            justFiredDragEnd = false;
        }
    });
    return pager;
}

From source file:com.vivavu.dream.view.PagerContainer.java

@Override
protected void onFinishInflate() {
    try {// www .  j  a v a  2 s . c  om

        mPager = new ViewPager(mContext);
        int width = (int) getResources().getDimension(R.dimen.book_width_dp);
        int height = (int) getResources().getDimension(R.dimen.book_height_dp);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width, height,
                Gravity.CENTER_HORIZONTAL);
        mPager.setLayoutParams(layoutParams);
        mPager.setId(AndroidUtils.generateViewId());
        //mPager.setId(R.id.shelf_pager);
        this.addView(mPager, 0);
        /*mPager = (ViewPager) getChildAt(0);
        mPager.setOnPageChangeListener(this);*/
    } catch (Exception e) {
        throw new IllegalStateException("The root child of PagerContainer must be a ViewPager");
    }
}

From source file:com.telenav.expandablepager.ExpandablePager.java

private void init() {
    mPager = new ViewPager(getContext());
    mPager.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        boolean change = true;

        @Override//from  w ww .jav a  2s .  c  om
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (change) {
                if (onSliderStateChangeListener != null) {
                    onSliderStateChangeListener.onPageChanged(
                            getPage(mPager.getCurrentItem() + (position < mPager.getCurrentItem() ? -1 : 1)),
                            sliderState);
                    change = !change;
                }
            }
        }

        @Override
        public void onPageSelected(int position) {
            if (onItemSelectedListener != null)
                onItemSelectedListener.onItemSelected(((ExpandablePagerAdapter) mPager.getAdapter()).items,
                        position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            change = true;
        }
    });
    addView(mPager);
    getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            pinToBottom();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
            if (onSliderStateChangeListener != null) {
                onSliderStateChangeListener.onPageChanged(getPage(mPager.getCurrentItem()), sliderState);
            }
        }
    });
}

From source file:org.birthdayadapter.ui.BaseActivity.java

/**
 * Called when the activity is first created.
 *///ww w  .j  av  a  2 s.c  o m
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mActivity = this;

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // Load Activity for Android < 4.0
        Intent oldActivity = new Intent(mActivity, BaseActivityV8.class);
        oldActivity.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(oldActivity);
        finish();
    } else {
        // Load new design with tabs
        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);

        setContentView(mViewPager);

        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(false);

        mTabsAdapter = new TabsAdapter(this, mViewPager);

        mTabsAdapter.addTab(actionBar.newTab().setText(getString(R.string.tab_main)), BaseFragment.class, null);

        mTabsAdapter.addTab(actionBar.newTab().setText(getString(R.string.tab_preferences)),
                PreferencesFragment.class, null);

        mTabsAdapter.addTab(actionBar.newTab().setText(getString(R.string.tab_accounts)),
                AccountListFragment.class, null);

        mTabsAdapter.addTab(actionBar.newTab().setText(getString(R.string.tab_help)), HelpFragment.class, null);

        mTabsAdapter.addTab(actionBar.newTab().setText(getString(R.string.tab_about)), AboutFragment.class,
                null);

        // default is disabled:
        mActivity.setProgressBarIndeterminateVisibility(Boolean.FALSE);

        mySharedPreferenceChangeListener = new MySharedPreferenceChangeListener(mActivity,
                mBackgroundStatusHandler);

        /*
         * Show workaround dialog for Android bug http://code.google.com/p/android/issues/detail?id=34880
         * Bug exists on Android 4.1 (SDK 16) and on some phones like Galaxy S4
         */
        if (BuildConfig.GOOGLE_PLAY_VERSION && PreferencesHelper.getShowWorkaroundDialog(mActivity)
                && !isPackageInstalled("org.birthdayadapter.jb.workaround")) {
            if ((Build.VERSION.SDK_INT == 16) || Build.DEVICE.toUpperCase().startsWith("GT-I9000")
                    || Build.DEVICE.toUpperCase().startsWith("GT-I9500")) {
                InstallWorkaroundDialogFragment dialog = InstallWorkaroundDialogFragment.newInstance();
                dialog.show(getFragmentManager(), "workaroundDialog");
            }
        }
    }
}

From source file:jfabrix101.lib.fragmentActivity.AbstractFragmentContentPager.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    internalViewPager = new ViewPager(getActivity());
    internalViewPager.setBackgroundColor(Color.RED);
    internalViewPager.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    initializeView(internalViewPager);//from  w w  w. j a v  a 2 s.c  o  m
    return internalViewPager;
}