Android Open Source - Rashr Navigation Drawer Fragment






From Project

Back to project page Rashr.

License

The source code is released under:

GNU General Public License

If you think the Android project Rashr 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 de.mkrtchyan.recoverytools;
/*from   ww w.  j  a v a 2s.c o m*/
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Copyright (c) 2014 Aschot Mkrtchyan
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
public class NavigationDrawerFragment extends Fragment {

    /**
     * A pointer to the current callbacks instance (the Activity).
     */
    private NavigationDrawerCallbacks mCallbacks;

    /**
     * Helper component that ties the action bar to the navigation drawer.
     */
    private ActionBarDrawerToggle mDrawerToggle;

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerListView;
    private View mFragmentContainerView;

    public NavigationDrawerFragment() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Indicate that this fragment would like to influence the set of actions in the action bar.
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        });
        return mDrawerListView;
    }

    public boolean isDrawerOpen() {
        return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
    }

    public boolean openDrawer() {
        mDrawerLayout.openDrawer(Gravity.LEFT);
        return mDrawerLayout != null;
    }

    public boolean closeDrawer() {
        mDrawerLayout.closeDrawer(Gravity.LEFT);
        return mDrawerLayout != null;
    }

    /**
     * Users of this fragment must call this method to set up the navigation drawer interactions.
     *
     * @param fragmentId   The android:id of this fragment in its activity's layout.
     * @param drawerLayout The DrawerLayout containing this fragment's UI.
     */
    public void setUp(int fragmentId, DrawerLayout drawerLayout) {
        mFragmentContainerView = getActivity().findViewById(fragmentId);

        mDrawerLayout = drawerLayout;

        mDrawerListView.setAdapter(new ArrayAdapter<>(
                getSupportActivity().getSupportActionBar().getThemedContext(),
                R.layout.drawer_list_item,
                android.R.id.text1,
                new String[]{
                        getString(R.string.flasher),
                        getString(R.string.recovery_script_manager),
                        getString(R.string.donate),
                        getString(R.string.settings),
                        getString(R.string.xda)
                }));

        // between the navigation drawer and the action bar app icon.
        mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                    /* host Activity */
                mDrawerLayout,                    /* DrawerLayout object */
                getSupportActivity().getToolbar(),
                R.string.app_name,                /* "open drawer" description for accessibility */
                R.string.app_name                 /* "close drawer" description for accessibility */
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (!isAdded()) {
                    return;
                }
                getSupportActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!isAdded()) {
                    return;
                }
                getSupportActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }
        };

        // Defer code dependent on restoration of previous instance state.
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    private void selectItem(int position) {
        if (mDrawerListView != null) {
            for (int i = 0; i < mDrawerListView.getChildCount(); i++) {
                View v = mDrawerListView.getChildAt(i);
                if (i != position) {
                    v.setBackgroundColor(getResources().getColor(R.color.darker_green));
                } else {
                    v.setBackgroundColor(getResources().getColor(R.color.custom_green));
                }
            }
            mDrawerListView.setItemChecked(position, true);
        }
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null) {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallbacks = (NavigationDrawerCallbacks) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    /**
     * Callbacks interface that all activities using this fragment must implement.
     */
    public static interface NavigationDrawerCallbacks {
        /**
         * Called when an item in the navigation drawer is selected.
         */
        void onNavigationDrawerItemSelected(int position);
    }

    private RashrActivity getSupportActivity() {
        return (RashrActivity)getActivity();
    }
}




Java Source Code List

com.fima.cardsui.StackAdapter.java
com.fima.cardsui.SwipeDismissTouchListener.java
com.fima.cardsui.Utils.java
com.fima.cardsui.objects.AbstractCard.java
com.fima.cardsui.objects.CardFactory.java
com.fima.cardsui.objects.CardModel.java
com.fima.cardsui.objects.CardStack.java
com.fima.cardsui.objects.Card.java
com.fima.cardsui.objects.RecyclableCard.java
com.fima.cardsui.views.CardUI.java
com.fima.cardsui.views.MyCard.java
com.fima.cardsui.views.MyImageCard.java
com.fima.cardsui.views.MyPlayCard.java
com.fima.cardsui.views.QuickReturnListView.java
de.mkrtchyan.recoverytools.BackupRestoreFragment.java
de.mkrtchyan.recoverytools.Constants.java
de.mkrtchyan.recoverytools.Device.java
de.mkrtchyan.recoverytools.FlashAsFragment.java
de.mkrtchyan.recoverytools.FlashFragment.java
de.mkrtchyan.recoverytools.FlashUtil.java
de.mkrtchyan.recoverytools.NavigationDrawerFragment.java
de.mkrtchyan.recoverytools.RashrActivity.java
de.mkrtchyan.recoverytools.ReportDialog.java
de.mkrtchyan.recoverytools.ScriptManagerFragment.java
de.mkrtchyan.recoverytools.SettingsFragment.java
de.mkrtchyan.utils.Common.java
de.mkrtchyan.utils.Downloader.java
de.mkrtchyan.utils.FileChooserDialog.java
de.mkrtchyan.utils.FileListView.java
de.mkrtchyan.utils.Notifyer.java
de.mkrtchyan.utils.SHA1.java
de.mkrtchyan.utils.Unzipper.java
donations.DonationsFragment.java
donations.google.util.Base64DecoderException.java
donations.google.util.Base64.java
donations.google.util.IabException.java
donations.google.util.IabHelper.java
donations.google.util.IabResult.java
donations.google.util.Inventory.java
donations.google.util.Purchase.java
donations.google.util.Security.java
donations.google.util.SkuDetails.java
org.sufficientlysecure.rootcommands.Mount.java
org.sufficientlysecure.rootcommands.Remounter.java
org.sufficientlysecure.rootcommands.RootCommands.java
org.sufficientlysecure.rootcommands.Shell.java
org.sufficientlysecure.rootcommands.SystemCommands.java
org.sufficientlysecure.rootcommands.Toolbox.java
org.sufficientlysecure.rootcommands.command.BinaryCommand.java
org.sufficientlysecure.rootcommands.command.Command.java
org.sufficientlysecure.rootcommands.command.SimpleBinaryCommand.java
org.sufficientlysecure.rootcommands.command.SimpleCommand.java
org.sufficientlysecure.rootcommands.util.BrokenBusyboxException.java
org.sufficientlysecure.rootcommands.util.FailedExecuteCommand.java
org.sufficientlysecure.rootcommands.util.Log.java
org.sufficientlysecure.rootcommands.util.RootAccessDeniedException.java
org.sufficientlysecure.rootcommands.util.UnsupportedArchitectureException.java
org.sufficientlysecure.rootcommands.util.Utils.java