Android Open Source - android-menudrawer Fragment Sample






From Project

Back to project page android-menudrawer.

License

The source code is released under:

Apache License

If you think the Android project android-menudrawer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.simonvt.menudrawer.samples;
/*from w w  w .j  a  va  2  s.co  m*/
import net.simonvt.menudrawer.MenuDrawer;
import net.simonvt.menudrawer.Position;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentSample extends BaseListSample {

    private static final String STATE_CURRENT_FRAGMENT = "net.simonvt.menudrawer.samples.FragmentSample";

    private FragmentManager mFragmentManager;
    private FragmentTransaction mFragmentTransaction;

    private String mCurrentFragmentTag;

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

        mFragmentManager = getSupportFragmentManager();

        if (inState != null) {
            mCurrentFragmentTag = inState.getString(STATE_CURRENT_FRAGMENT);
        } else {
            mCurrentFragmentTag = ((Item) mAdapter.getItem(0)).mTitle;
            attachFragment(mMenuDrawer.getContentContainer().getId(), getFragment(mCurrentFragmentTag),
                    mCurrentFragmentTag);
            commitTransactions();
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }

        mMenuDrawer.setOnDrawerStateChangeListener(new MenuDrawer.OnDrawerStateChangeListener() {
            @Override
            public void onDrawerStateChange(int oldState, int newState) {
                if (newState == MenuDrawer.STATE_CLOSED) {
                    commitTransactions();
                }
            }

            @Override
            public void onDrawerSlide(float openRatio, int offsetPixels) {
                // Do nothing
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(STATE_CURRENT_FRAGMENT, mCurrentFragmentTag);
    }

    @Override
    protected void onMenuItemClicked(int position, Item item) {
        if (mCurrentFragmentTag != null) detachFragment(getFragment(mCurrentFragmentTag));
        attachFragment(mMenuDrawer.getContentContainer().getId(), getFragment(item.mTitle), item.mTitle);
        mCurrentFragmentTag = item.mTitle;
        mMenuDrawer.closeMenu();
    }

    @Override
    protected int getDragMode() {
        return MenuDrawer.MENU_DRAG_WINDOW;
    }

    @Override
    protected Position getDrawerPosition() {
        return Position.LEFT;
    }

    protected FragmentTransaction ensureTransaction() {
        if (mFragmentTransaction == null) {
            mFragmentTransaction = mFragmentManager.beginTransaction();
            mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        }

        return mFragmentTransaction;
    }

    private Fragment getFragment(String tag) {
        Fragment f = mFragmentManager.findFragmentByTag(tag);

        if (f == null) {
            f = SampleFragment.newInstance(tag);
        }

        return f;
    }

    protected void attachFragment(int layout, Fragment f, String tag) {
        if (f != null) {
            if (f.isDetached()) {
                ensureTransaction();
                mFragmentTransaction.attach(f);
            } else if (!f.isAdded()) {
                ensureTransaction();
                mFragmentTransaction.add(layout, f, tag);
            }
        }
    }

    protected void detachFragment(Fragment f) {
        if (f != null && !f.isDetached()) {
            ensureTransaction();
            mFragmentTransaction.detach(f);
        }
    }

    protected void commitTransactions() {
        if (mFragmentTransaction != null && !mFragmentTransaction.isEmpty()) {
            mFragmentTransaction.commit();
            mFragmentTransaction = null;
        }
    }

    public static class SampleFragment extends Fragment {

        private static final String ARG_TEXT = "net.simonvt.menudrawer.samples.SampleFragment.text";

        public static SampleFragment newInstance(String text) {
            SampleFragment f = new SampleFragment();

            Bundle args = new Bundle();
            args.putString(ARG_TEXT, text);
            f.setArguments(args);

            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_sample, container, false);

            ((TextView) v.findViewById(R.id.text)).setText(getArguments().getString(ARG_TEXT));

            return v;
        }
    }
}




Java Source Code List

net.simonvt.menudrawer.BuildLayerFrameLayout.java
net.simonvt.menudrawer.ColorDrawable.java
net.simonvt.menudrawer.DraggableDrawer.java
net.simonvt.menudrawer.FloatScroller.java
net.simonvt.menudrawer.MenuDrawer.java
net.simonvt.menudrawer.NoClickThroughFrameLayout.java
net.simonvt.menudrawer.OverlayDrawer.java
net.simonvt.menudrawer.PeekInterpolator.java
net.simonvt.menudrawer.Position.java
net.simonvt.menudrawer.Scroller.java
net.simonvt.menudrawer.SinusoidalInterpolator.java
net.simonvt.menudrawer.SlideDrawable.java
net.simonvt.menudrawer.SlidingDrawer.java
net.simonvt.menudrawer.SmoothInterpolator.java
net.simonvt.menudrawer.StaticDrawer.java
net.simonvt.menudrawer.ViewHelper.java
net.simonvt.menudrawer.compat.ActionBarHelperCompat.java
net.simonvt.menudrawer.compat.ActionBarHelperNative.java
net.simonvt.menudrawer.compat.ActionBarHelper.java
net.simonvt.menudrawer.samples.ActionBarSherlockSample.java
net.simonvt.menudrawer.samples.BaseListSample.java
net.simonvt.menudrawer.samples.BottomDrawerSample.java
net.simonvt.menudrawer.samples.BottomOverlaySample.java
net.simonvt.menudrawer.samples.Category.java
net.simonvt.menudrawer.samples.FragmentSample.java
net.simonvt.menudrawer.samples.Item.java
net.simonvt.menudrawer.samples.LayoutSample.java
net.simonvt.menudrawer.samples.LeftDrawerSample.java
net.simonvt.menudrawer.samples.LeftOverlaySample.java
net.simonvt.menudrawer.samples.ListActivitySample.java
net.simonvt.menudrawer.samples.MenuAdapter.java
net.simonvt.menudrawer.samples.RightDrawerSample.java
net.simonvt.menudrawer.samples.RightOverlaySample.java
net.simonvt.menudrawer.samples.SamplesActivity.java
net.simonvt.menudrawer.samples.StaticDrawerSample.java
net.simonvt.menudrawer.samples.TopDrawerSample.java
net.simonvt.menudrawer.samples.TopOverlaySample.java
net.simonvt.menudrawer.samples.ViewPagerSample.java
net.simonvt.menudrawer.samples.WindowSample.java