Example usage for android.support.v4.app Fragment Fragment

List of usage examples for android.support.v4.app Fragment Fragment

Introduction

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

Prototype

Fragment

Source Link

Usage

From source file:org.dmfs.android.microfragments.transitions.ForwardResetTransition.java

@Override
public void prepare(@NonNull Context context, @NonNull FragmentManager fragmentManager,
        @NonNull MicroFragmentHost host, @NonNull MicroFragment<?> previousStep) {
    // insert an empty dummy fragment to enforce the animation that we want, otherwise the pop animation of the curent fragment would be played which is usually not what we want
    fragmentManager.beginTransaction()//from  ww  w  .  java 2 s .c  o  m
            .setCustomAnimations(R.anim.microfragments_swipe_enter, R.anim.microfragments_swipe_exit,
                    R.anim.microfragments_swipe_enter, R.anim.microfragments_swipe_exit)
            .replace(R.id.microfragments_host, new Fragment()).commit();
    fragmentManager.executePendingTransactions();
    fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

From source file:org.xbmc.android.view.RelativePagerAdapter.java

@Override
public Fragment getItem(int position) {
    if (currFragment == null) {
        throw new RuntimeException("Must set initial fragment before view is rendered.");
    }//from ww  w .  j  a va2 s  .c om
    if (position == PAGE_POSITION_RIGHT) {
        return currFragment.hasNext() ? currFragment.getNext(fragmentStateManager) : new Fragment();
    } else if (position == PAGE_POSITION_LEFT) {
        return currFragment.hasPrev() ? currFragment.getPrev(fragmentStateManager) : new Fragment();
    } else {
        return currFragment;
    }
}

From source file:com.example.slidingmenu.fragment.NewsFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.e(TAG, "onCreateView");
    View mView = inflater.inflate(R.layout.view_pager, null);

    showLeft = (View) mView.findViewById(R.id.head_layout_showLeft);
    showRight = (View) mView.findViewById(R.id.head_layout_showRight);

    mTopTitleView = (TextView) showLeft.findViewById(R.id.head_layout_text);
    mTopTitleView.setText(getString(R.string.tab_news));
    mTopBackView = (ImageView) showLeft.findViewById(R.id.head_layout_back);
    mTopBackView.setBackgroundResource(R.drawable.biz_news_main_back_normal);

    mPager = (ViewPager) mView.findViewById(R.id.vp_list);

    listViewPagerAdapter = new ListViewPagerAdapter(this);
    mPager.setAdapter(listViewPagerAdapter);

    pagerItemList = new ArrayList<Fragment>();
    pagerItemList.add(new Fragment());
    pagerItemList.add(new Fragment());
    pagerItemList.add(new Fragment());
    pagerItemList.add(new Fragment());
    pagerItemList.add(new Fragment());
    pagerItemList.add(new Fragment());
    pagerItemList.add(new Fragment());

    //      PageFragment1 page1 = new PageFragment1();
    //      PageFragment2 page2 = new PageFragment2();
    //      pagerItemList.add(page1);
    //      pagerItemList.add(page2);
    //      mAdapter = new MyAdapter(getFragmentManager());
    //      mPager.setAdapter(mAdapter);

    mPager.setOnPageChangeListener(this);
    initScrollableTabs(mView, mPager);/*from  www .  j  a v a2  s. co  m*/
    return mView;
}

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  ww  . j  av 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));
    }

}

From source file:org.que.activities.fragments.CalendarBookedFragment.java

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    String itemID = "NULL";
    Fragment frg = new Fragment();
    Bundle args = new Bundle();
    Object o = getListView().getItemAtPosition(position);
    super.onListItemClick(l, v, position, id);
    Cursor c = ((SimpleCursorAdapter) l.getAdapter()).getCursor();

    c.moveToPosition(position);// www  .  ja  v a2s.  c  o m
    String description = c.getString(5); //gets the description field of the calendar item
    try {

        //check whether item is a session or a paper
        if (description.contains("session")) {
            //itemID cleans up the description String first to get the Database ID of the Session
            itemID = description
                    .replace(getActivity().getString(R.string.calendarEventDescription) + "sessionID", "");
            SessionEntity entity = ConferenceDAO.getSessionByID(itemID, getActivity());
            args.putSerializable(SessionFragment.ARG_SESSIONVIEW_FRAGMENT, entity);
            frg = new SessionFragment();

        } else if (description.contains("paper")) {
            itemID = description.replace(getActivity().getString(R.string.calendarEventDescription) + "paperID",
                    "");
            PaperEntity entity = new PaperEntity();
            entity = ConferenceDAO.getPaperByID(itemID, getActivity());
            args.putSerializable(PaperFragment.ARG_PAPERVIEW_FRAGMENT, entity);
            frg = new PaperFragment();

        }
    } catch (NullPointerException e) {
        AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
        b.setMessage(
                "Something went wrong when trying to retrieve this event. Please re-favorite the event to re-add it to the calendar.");
        b.setTitle("Oops");
        b.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        b.show();
    }
    if (frg != null) {
        frg.setArguments(args);
        FragmentManager mgr = ((FragmentActivity) getActivity()).getSupportFragmentManager();
        Fragment old = mgr.findFragmentById(R.id.content_frame);

        FragmentTransaction trx = mgr.beginTransaction();
        if (old != null)
            trx.remove(old);

        trx.add(R.id.content_frame, frg).addToBackStack(null) //TODO
                .commit();
    }
}

From source file:com.money.manager.ex.budget.BudgetsActivity.java

/**
 * Displays the fragment and associate the tag
 *
 * @param fragment//from   ww  w .  ja  v a  2 s .co  m
 * @param tagFragment
 */
public void showFragment(Fragment fragment, String tagFragment) {
    // In tablet layout, do not try to display the Home Fragment again. Show empty fragment.
    if (isDualPanel() && tagFragment.equalsIgnoreCase(BudgetListFragment.class.getName())) {
        fragment = new Fragment();
        tagFragment = "Empty";
    }

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_right,
            R.anim.slide_out_left);
    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack.
    if (isDualPanel()) {
        transaction.replace(R.id.fragmentDetail, fragment, tagFragment);
    } else {
        // Single panel UI.
        transaction.replace(R.id.fragmentMain, fragment, tagFragment);

        // todo: enable going back only if showing the list.
        //            boolean showingList = tagFragment.equals(BudgetListFragment.class.getName());
        //            setDisplayHomeAsUpEnabled(showingList);
    }
    transaction.addToBackStack(null);
    // Commit the transaction
    transaction.commit();
}

From source file:com.tafayor.selfcamerashot.ui.FragmentWrapperActivity.java

private void loadFragment(int id) {

    Fragment fragment = new Fragment();
    if (id == FRAGMENT_ABOUT) {

        fragment = new AboutFragment();
    }/*from www  .  j  a v  a 2 s. com*/

    getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, fragment).commit();
}

From source file:de.Maxr1998.xposed.maxlock.ui.settings.appslist.AppListAdapter.java

@Override
public void onBindViewHolder(final AppsListViewHolder hld, final int position) {
    final String sTitle = (String) mItemList.get(position).get("title");
    final String key = (String) mItemList.get(position).get("key");
    final Drawable dIcon = (Drawable) mItemList.get(position).get("icon");

    hld.appName.setText(sTitle);/*  w  w  w. j  ava2s  .  c o  m*/
    hld.appIcon.setImageDrawable(dIcon);

    if (prefsPackages.getBoolean(key, false)) {
        hld.toggle.setChecked(true);
        hld.options.setVisibility(View.VISIBLE);
    } else {
        hld.toggle.setChecked(false);
        hld.options.setVisibility(View.GONE);
    }

    hld.appIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (key.equals("com.android.packageinstaller"))
                return;
            Intent it = mContext.getPackageManager().getLaunchIntentForPackage(key);
            it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(it);
        }
    });

    hld.options.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // AlertDialog View
            // Fake die checkbox
            View checkBoxView = View.inflate(mContext, R.layout.per_app_settings, null);
            CheckBox fakeDie = (CheckBox) checkBoxView.findViewById(R.id.cb_fake_die);
            fakeDie.setChecked(prefsPackages.getBoolean(key + "_fake", false));
            fakeDie.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    boolean value = cb.isChecked();
                    prefsPackages.edit().putBoolean(key + "_fake", value).commit();
                }
            });
            // Custom password checkbox
            CheckBox customPassword = (CheckBox) checkBoxView.findViewById(R.id.cb_custom_pw);
            customPassword.setChecked(prefsPerApp.contains(key));
            customPassword.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    boolean checked = cb.isChecked();
                    if (checked) {
                        dialog.dismiss();
                        final AlertDialog.Builder choose_lock = new AlertDialog.Builder(mContext);
                        CharSequence[] cs = new CharSequence[] {
                                mContext.getString(R.string.pref_locking_type_password),
                                mContext.getString(R.string.pref_locking_type_pin),
                                mContext.getString(R.string.pref_locking_type_knockcode),
                                mContext.getString(R.string.pref_locking_type_pattern) };
                        choose_lock.setItems(cs, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                                Fragment frag = new Fragment();
                                switch (i) {
                                case 0:
                                    Util.setPassword(mContext, key);
                                    break;
                                case 1:
                                    frag = new PinSetupFragment();
                                    break;
                                case 2:
                                    frag = new KnockCodeSetupFragment();
                                    break;
                                case 3:
                                    Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN, null,
                                            mContext, LockPatternActivity.class);
                                    mFragment.startActivityForResult(intent, Util.getPatternCode(position));
                                    break;
                                }
                                if (i == 1 || i == 2) {
                                    Bundle b = new Bundle(1);
                                    b.putString(Common.INTENT_EXTRAS_CUSTOM_APP, key);
                                    frag.setArguments(b);
                                    ((SettingsActivity) mContext).getSupportFragmentManager().beginTransaction()
                                            .replace(R.id.frame_container, frag).addToBackStack(null).commit();
                                }
                            }
                        }).show();
                    } else
                        prefsPerApp.edit().remove(key).remove(key + Common.APP_KEY_PREFERENCE).apply();

                }
            });
            // Finish dialog
            dialog = new AlertDialog.Builder(mContext)
                    .setTitle(mContext.getString(R.string.dialog_title_settings)).setIcon(dIcon)
                    .setView(checkBoxView)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dlg, int id) {
                            dlg.dismiss();
                        }
                    }).setNeutralButton(R.string.dialog_button_exclude_activities,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    new ActivityLoader().execute(key);
                                }
                            })
                    .show();
        }
    });
    hld.toggle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioButton rb = (RadioButton) v;
            boolean value = prefsPackages.getBoolean(key, false);

            if (!value) {
                prefsPackages.edit().putBoolean(key, true).commit();
                AnimationSet anim = new AnimationSet(true);
                anim.addAnimation(AnimationUtils.loadAnimation(mContext, R.anim.appslist_settings_rotate));
                anim.addAnimation(AnimationUtils.loadAnimation(mContext, R.anim.appslist_settings_translate));
                hld.options.startAnimation(anim);
                hld.options.setVisibility(View.VISIBLE);
                rb.setChecked(true);
            } else {
                prefsPackages.edit().remove(key).commit();
                rb.setChecked(true);

                AnimationSet animOut = new AnimationSet(true);
                animOut.addAnimation(
                        AnimationUtils.loadAnimation(mContext, R.anim.appslist_settings_rotate_out));
                animOut.addAnimation(
                        AnimationUtils.loadAnimation(mContext, R.anim.appslist_settings_translate_out));
                animOut.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
                        animation.setDuration(1);
                        hld.options.startAnimation(animation);
                        hld.options.setVisibility(View.GONE);
                        hld.options.clearAnimation();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                hld.options.startAnimation(animOut);
            }

            notifyDataSetChanged();
        }

    });
}

From source file:com.example.NvDrawerListWithTabHostDataPassSwipe.MainActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments

    Fragment fragment = null;//from   w  w w  .  j a  v a 2 s .  c o m

    switch (position) {

    case 0: // Transaction

        fragment = new TransactionFragmentTabHostSwipe();
        break;

    default:

        fragment = new Fragment();

    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mLeftDrawerListView.setItemChecked(position, true);
    setTitle(mLeftMenu[position]);
    mDrawerLayout.closeDrawer(mLeftDrawerListView);
}

From source file:com.example.nvdrawerlistwithtabhostswipe.MainActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments

    Fragment fragment = null;/*from  ww w  .j a va  2  s .c  o m*/

    switch (position) {
    case 0: // Profile

        fragment = new ProfileFragmentTabHostSwipe();
        break;

    case 1: // Transaction

        fragment = new TransactionFragmentTabHostSwipe();
        break;

    default:

        fragment = new Fragment();

    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mLeftDrawerListView.setItemChecked(position, true);
    setTitle(mLeftMenu[position]);
    mDrawerLayout.closeDrawer(mLeftDrawerListView);
}