1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.newsreader; 18 19 import android.app.ActionBar.OnNavigationListener; 20 import android.app.ActionBar.Tab; 21 import android.app.ActionBar.TabListener; 22 import android.app.FragmentTransaction; 23 24 /** 25 * Adapter for action bar navigation events. 26 * 27 * This class implements an adapter that facilitates handling of action bar navigation events. 28 * An instance of this class must be installed as a TabListener or OnNavigationListener on an 29 * Action Bar, and it will relay the navigation events to a configured listener 30 * (a {@link CompatActionBarNavListener}). 31 * 32 * This class should only be instanced and used on Android platforms that support the Action Bar, 33 * that is, SDK level 11 and above. 34 */ 35 public class CompatActionBarNavHandler implements TabListener, OnNavigationListener { 36 // The listener that we notify of navigation events 37 CompatActionBarNavListener mNavListener; 38 39 /** 40 * Constructs an instance with the given listener. 41 * 42 * @param listener the listener to notify when a navigation event occurs. 43 */ CompatActionBarNavHandler(CompatActionBarNavListener listener)44 public CompatActionBarNavHandler(CompatActionBarNavListener listener) { 45 mNavListener = listener; 46 } 47 48 /** 49 * Called by framework when a tab is selected. 50 * 51 * This will cause a navigation event to be delivered to the configured listener. 52 */ 53 @Override onTabSelected(Tab tab, FragmentTransaction ft)54 public void onTabSelected(Tab tab, FragmentTransaction ft) { 55 // TODO Auto-generated method stub 56 mNavListener.onCategorySelected(tab.getPosition()); 57 } 58 59 /** 60 * Called by framework when a item on the navigation menu is selected. 61 * 62 * This will cause a navigation event to be delivered to the configured listener. 63 */ 64 @Override onNavigationItemSelected(int itemPosition, long itemId)65 public boolean onNavigationItemSelected(int itemPosition, long itemId) { 66 mNavListener.onCategorySelected(itemPosition); 67 return true; 68 } 69 70 71 /** 72 * Called by framework when a tab is re-selected. That is, it was already selected and is 73 * tapped on again. This is not used in our app. 74 */ 75 @Override onTabReselected(Tab tab, FragmentTransaction ft)76 public void onTabReselected(Tab tab, FragmentTransaction ft) { 77 // we don't care 78 } 79 80 /** 81 * Called by framework when a tab is unselected. Not used in our app. 82 */ 83 @Override onTabUnselected(Tab tab, FragmentTransaction ft)84 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 85 // we don't care 86 } 87 88 } 89