Android Open Source - nxt-remote-controller Navigation Drawer Fragment






From Project

Back to project page nxt-remote-controller.

License

The source code is released under:

MIT License

If you think the Android project nxt-remote-controller 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

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  Copyright (c) 2014 EgaTuts & Esa Garca - All Rights Reserved                 *
 *                                                                                 *
 *  Open-source code licensed under the MIT License (the "License").               *
 *                                                                                 *
 *  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.                                                                  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
//  w  ww.  j  a  v  a2  s .c o m
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  You can find the entire project at:                                                                                                                                      *
 *                                                                                                                                                                           *
 *    https://github.com/Egatuts/nxt-remote-controller                                                                                                                       *
 *                                                                                                                                                                           *
 *  And the corresponding file at:                                                                                                                                           *
 *                                                                                                                                                                           *
 *    https://github.com/Egatuts/nxt-remote-controller/blob/master/Android%20App/app/src/main/java/git/egatuts/nxtremotecontroller/navigation/NavigationDrawerFragment.java  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package git.egatuts.nxtremotecontroller.navigation;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import git.egatuts.nxtremotecontroller.R;

/*
 *  Fragment used to create the drawer menu.
 */
public class NavigationDrawerFragment extends Fragment implements NavigationDrawerCallback {

  private static final String CURRENT_POSITION = "drawer_current_position";

  private View fragment_container_view;
  private DrawerLayout drawer_layout;

  private ActionBarDrawerToggle drawer_toggle;

  private int current_position;
  private NavigationDrawerCallback custom_callback;

  View inflated_view;
  LinearLayoutManager layout_manager;
  RecyclerView drawer_list;
  NavigationDrawerAdapter navigation_drawer_adapter;

  /*
   *  Getter and setter for drawer layout.
   */
  public void setDrawerLayout (DrawerLayout layout) {
    drawer_layout = layout;
  }

  public DrawerLayout getDrawerLayout () {
    return drawer_layout;
  }

  /*
   *  Getter and setter for action bar drawer toggle.
   */
  public void setActionBarDrawerToggle (ActionBarDrawerToggle custom_drawer_toggle) {
    drawer_toggle = custom_drawer_toggle;
  }

  public ActionBarDrawerToggle getActionBarDrawerToggle () {
    return drawer_toggle;
  }

  /*
   *  onDetach and onAttach methods.
   */
  @Override
  public void onDetach () {
    super.onDetach();
    custom_callback = null;
  }

  @Override
  public void onAttach (Activity custom_activity) {
    super.onAttach(custom_activity);
    try {
      custom_callback = (NavigationDrawerCallback) custom_activity;
    } catch (ClassCastException e) {
      throw new ClassCastException("Activity MUST implement git.egatuts.nxtremotecontroller.NavigationDrawerCallback interface.");
    }
  }

  /*
   *  Drawer opener, closer and checker.
   */
  public void openDrawer () {
    drawer_layout.openDrawer(fragment_container_view);
  }

  public void closeDrawer () {
    drawer_layout.closeDrawer(fragment_container_view);
  }

  public boolean isDrawerOpened () {
    return drawer_layout != null && drawer_layout.isDrawerOpen(fragment_container_view);
  }

  /*
   *  Select item and executes custom callback.
   */
  public void selectItem (int position) {
    current_position = position;
    if (drawer_layout != null) {
      drawer_layout.closeDrawer(fragment_container_view);
    }
    if (custom_callback != null) {
      custom_callback.onNavigationDrawerItemSelected(position);
    }
  }

  @Override
  public void onNavigationDrawerItemSelected (int position) {
    selectItem(position);
  }

  @Override public void onCloseDrawer () {}
  @Override public void onOpenDrawer () {}

  /*
   *  onConfigurationChanged and onSaveInstanceState methods.
   *  Both save the actual state or configuration when the Activity is destroyed or paused.
   */
  @Override
  public void onConfigurationChanged (Configuration config) {
    super.onConfigurationChanged(config);
    drawer_toggle.onConfigurationChanged(config);
  }

  @Override
  public void onSaveInstanceState (Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(CURRENT_POSITION, current_position);
  }

  /*
   *  onCreate and onCreateView methods.
   */
  @Override
  public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
      current_position = savedInstanceState.getInt(CURRENT_POSITION);
    }
  }

  @Override
  public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    inflated_view = inflater.inflate(R.layout.navigation_drawer_fragment, container, false);
    drawer_list = (RecyclerView) inflated_view.findViewById(R.id.drawer_list);
    layout_manager = new LinearLayoutManager(getActivity());

    layout_manager.setOrientation(LinearLayoutManager.VERTICAL);
    drawer_list.setLayoutManager(layout_manager);
    drawer_list.setHasFixedSize(true);

    final List<DrawerItem> drawer_menu = getMenu();
    navigation_drawer_adapter = new NavigationDrawerAdapter(drawer_menu);
    navigation_drawer_adapter.setNavigationDrawerCallback(this);
    drawer_list.setAdapter(navigation_drawer_adapter);
    selectItem(current_position);
    return inflated_view;
  }

  public void setup (int fragment_id, DrawerLayout custom_drawer_layout, Toolbar toolbar) {
    fragment_container_view = getActivity().findViewById(fragment_id);
    drawer_layout = custom_drawer_layout;

    drawer_toggle = new ActionBarDrawerToggle(getActivity(), drawer_layout, toolbar, R.string.drawer_open, R.string.drawer_close) {
      @Override
        public void onDrawerClosed (View drawerView) {
        super.onDrawerClosed(drawerView);
        if (!isAdded()) return;
        if (custom_callback != null) custom_callback.onCloseDrawer();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
          getActivity().invalidateOptionsMenu();
      }

      @Override
      public void onDrawerOpened (View drawerView) {
        super.onDrawerOpened(drawerView);
        if (!isAdded()) return;
        if (custom_callback != null) custom_callback.onOpenDrawer();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
          getActivity().invalidateOptionsMenu();
      }
    };

    drawer_layout.post(new Runnable() {
      @Override
      public void run () {
        drawer_toggle.syncState();
      }
    });

    drawer_layout.setDrawerListener(drawer_toggle);
  }

  /*
   *  Strings, drawables and menu getters.
   */
  private String getStr (int id) {
    return getResources().getString(id);
  }

  private Drawable getDrw (int id) {
    return getResources().getDrawable(id);
  }

  public List<DrawerItem> getMenu () {
    List<DrawerItem> items = new ArrayList<DrawerItem>();
    items.add(new DrawerItem(getStr(R.string.home_drawer_label), getDrw(R.drawable.ic_home)));
    items.add(new DrawerItem(getStr(R.string.scan_drawer_label), getDrw(R.drawable.ic_scan)));
    items.add(new DrawerItem(getStr(R.string.settings_drawer_label), getDrw(R.drawable.ic_preferences)));
    items.add(new DrawerItem(getStr(R.string.help_drawer_label), getDrw(R.drawable.ic_help)));
    items.add(new DrawerItem(getStr(R.string.donations_drawer_label), getDrw(R.drawable.ic_paypal)));
    return items;
  }

}




Java Source Code List

.OldPadControllerFragment.java
com.andexert.library.ApplicationTest.java
com.andexert.library.RippleView.java
com.andexert.rippleeffect.ApplicationTest.java
com.andexert.rippleeffect.CustomAdapter.java
com.andexert.rippleeffect.MainActivity.java
com.andexert.rippleeffect.OnTapListener.java
com.gc.materialdesign.utils.Utils.java
com.gc.materialdesign.views.ButtonFlat.java
com.gc.materialdesign.views.ButtonFloatSmall.java
com.gc.materialdesign.views.ButtonFloat.java
com.gc.materialdesign.views.ButtonIcon.java
com.gc.materialdesign.views.ButtonRectangle.java
com.gc.materialdesign.views.Button.java
com.gc.materialdesign.views.Card.java
com.gc.materialdesign.views.CheckBox.java
com.gc.materialdesign.views.CustomView.java
com.gc.materialdesign.views.LayoutRipple.java
com.gc.materialdesign.views.ProgressBarCircularIndeterminate.java
com.gc.materialdesign.views.ProgressBarDeterminate.java
com.gc.materialdesign.views.ProgressBarIndeterminateDeterminate.java
com.gc.materialdesign.views.ProgressBarIndeterminate.java
com.gc.materialdesign.views.RippleView.java
com.gc.materialdesign.views.ScrollView.java
com.gc.materialdesign.views.Slider.java
com.gc.materialdesign.views.Switch.java
com.gc.materialdesign.widgets.ColorSelector.java
com.gc.materialdesign.widgets.Dialog.java
com.gc.materialdesign.widgets.SnackBar.java
git.egatuts.nxtremotecontroller.ApplicationTest.java
git.egatuts.nxtremotecontroller.GlobalUtils.java
git.egatuts.nxtremotecontroller.activity.ActivityPendingTransition.java
git.egatuts.nxtremotecontroller.activity.BaseActivity.java
git.egatuts.nxtremotecontroller.activity.ControllerActivity.java
git.egatuts.nxtremotecontroller.activity.DefaultActivityPendingTransition.java
git.egatuts.nxtremotecontroller.activity.MainActivity.java
git.egatuts.nxtremotecontroller.activity.SettingsActivity.java
git.egatuts.nxtremotecontroller.bluetooth.BluetoothConstants.java
git.egatuts.nxtremotecontroller.bluetooth.BluetoothUtils.java
git.egatuts.nxtremotecontroller.bluetooth.NXTConnector.java
git.egatuts.nxtremotecontroller.device.PairedDeviceAdapter.java
git.egatuts.nxtremotecontroller.device.PairedDeviceItemClickListener.java
git.egatuts.nxtremotecontroller.device.PairedDeviceViewHolder.java
git.egatuts.nxtremotecontroller.device.PairedDevice.java
git.egatuts.nxtremotecontroller.exception.SocketCreationException.java
git.egatuts.nxtremotecontroller.fragment.ActivityBaseFragment.java
git.egatuts.nxtremotecontroller.fragment.BaseFragment.java
git.egatuts.nxtremotecontroller.fragment.BluetoothFragment.java
git.egatuts.nxtremotecontroller.fragment.ControllerBaseFragment.java
git.egatuts.nxtremotecontroller.fragment.DefaultFragmentPendingTransition.java
git.egatuts.nxtremotecontroller.fragment.FragmentPendingTransition.java
git.egatuts.nxtremotecontroller.fragment.HomeFragment.java
git.egatuts.nxtremotecontroller.fragment.LocalControllerFragment.java
git.egatuts.nxtremotecontroller.fragment.OnlineControllerFragment.java
git.egatuts.nxtremotecontroller.fragment.ScanFragment.java
git.egatuts.nxtremotecontroller.fragment.SettingsFragment.java
git.egatuts.nxtremotecontroller.listener.AnimationEndListener.java
git.egatuts.nxtremotecontroller.listener.AppKillerListener.java
git.egatuts.nxtremotecontroller.listener.BaseListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothDiscoveryListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothEnableListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothPairingListener.java
git.egatuts.nxtremotecontroller.navigation.DrawerItemViewHolder.java
git.egatuts.nxtremotecontroller.navigation.DrawerItem.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerAdapter.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerCallback.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerFragment.java
git.egatuts.nxtremotecontroller.preference.PreferencesUtils.java
git.egatuts.nxtremotecontroller.receiver.AppKillerReceiver.java
git.egatuts.nxtremotecontroller.receiver.BaseReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothDiscoveryReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothEnableReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothPairingReceiver.java
git.egatuts.nxtremotecontroller.thread.BaseThread.java
git.egatuts.nxtremotecontroller.thread.ConnectThread.java
git.egatuts.nxtremotecontroller.thread.ConnectedThread.java
git.egatuts.nxtremotecontroller.views.BaseIndeterminateProgressDialog.java
git.egatuts.nxtremotecontroller.views.BaseProgressDialog.java
git.egatuts.nxtremotecontroller.views.JoystickView.java
git.egatuts.nxtremotecontroller.views.LongIndeterminateProgressDialog.java
git.egatuts.nxtremotecontroller.views.ShortIndeterminateProgressDialog.java