Skip to content
This repository
Browse code

Hide menu if press of the back button and menu is shown

  • Loading branch information...
commit 5d26c223ed824747c817e496d88dd15205dd0581 1 parent 252be2d
Evgeny Shishkin authored September 05, 2012
1  library/src/com/devspark/sidenavigation/ISideNavigationCallback.java
... ...
@@ -1,6 +1,7 @@
1 1
 package com.devspark.sidenavigation;
2 2
 
3 3
 /**
  4
+ * Callback interface for {@link SideNavigationView}.
4 5
  * 
5 6
  * @author e.shishkin
6 7
  *
39  library/src/com/devspark/sidenavigation/SideNavigationItem.java
... ...
@@ -0,0 +1,39 @@
  1
+package com.devspark.sidenavigation;
  2
+
  3
+/**
  4
+ * Item of side navigation.
  5
+ * 
  6
+ * @author johnkil
  7
+ * 
  8
+ */
  9
+class SideNavigationItem {
  10
+
  11
+	private int id;
  12
+	private int icon;
  13
+	private String text;
  14
+
  15
+	public int getId() {
  16
+		return id;
  17
+	}
  18
+
  19
+	public void setId(int id) {
  20
+		this.id = id;
  21
+	}
  22
+
  23
+	public int getIcon() {
  24
+		return icon;
  25
+	}
  26
+
  27
+	public void setIcon(int icon) {
  28
+		this.icon = icon;
  29
+	}
  30
+
  31
+	public String getText() {
  32
+		return text;
  33
+	}
  34
+
  35
+	public void setText(String text) {
  36
+		this.text = text;
  37
+	}
  38
+
  39
+}
113  library/src/com/devspark/sidenavigation/SideNavigationView.java
@@ -21,6 +21,7 @@
21 21
 import android.widget.TextView;
22 22
 
23 23
 /**
  24
+ * View of displaying side navigation.
24 25
  * 
25 26
  * @author e.shishkin
26 27
  * 
@@ -33,36 +34,42 @@
33 34
 	private View outsideView;
34 35
 
35 36
 	private ISideNavigationCallback callback;
  37
+	private ArrayList<SideNavigationItem> menuItems;
36 38
 
37  
-	private static ArrayList<SideNavigationItem> menuItems;
38  
-
  39
+	/**
  40
+	 * Constructor of {@link SideNavigationView}.
  41
+	 * @param context
  42
+	 */
39 43
 	public SideNavigationView(Context context) {
40 44
 		super(context);
41 45
 		load();
42 46
 	}
43 47
 
  48
+	/**
  49
+	 * Constructor of {@link SideNavigationView}.
  50
+	 * @param context
  51
+	 * @param attrs
  52
+	 */
44 53
 	public SideNavigationView(Context context, AttributeSet attrs) {
45 54
 		super(context, attrs);
46 55
 		load();
47 56
 	}
48 57
 
  58
+	/**
  59
+	 * Loading of side navigation view.
  60
+	 */
49 61
 	private void load() {
50 62
 		if (isInEditMode()) {
51 63
 			return;
52 64
 		}
53  
-		inflateLayout();
54  
-		initUi();
55  
-	}
56  
-
57  
-	private void inflateLayout() {
58  
-		try {
59  
-			LayoutInflater.from(getContext()).inflate(R.layout.side_navigation, this, true);
60  
-		} catch (Exception e) {
61  
-			Log.w(LOG_TAG, e);
62  
-		}
  65
+		initView();
63 66
 	}
64 67
 
65  
-	private void initUi() {
  68
+	/**
  69
+	 * Initialization layout of side menu.
  70
+	 */
  71
+	private void initView() {
  72
+		LayoutInflater.from(getContext()).inflate(R.layout.side_navigation, this, true);
66 73
 		navigationMenu = (LinearLayout) findViewById(R.id.side_navigation_menu);
67 74
 		listView = (ListView) findViewById(R.id.side_navigation_listview);
68 75
 		outsideView = (View) findViewById(R.id.side_navigation_outside_view);
@@ -76,28 +83,42 @@ public void onClick(View v) {
76 83
 			@Override
77 84
 			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
78 85
 				if (callback != null) {
79  
-					callback.onSideNavigationItemClick(menuItems.get(position).id);
  86
+					callback.onSideNavigationItemClick(menuItems.get(position).getId());
80 87
 				}
81 88
 				hideMenu();
82 89
 			}
83 90
 		});
84 91
 	}
85 92
 
  93
+	/**
  94
+	 * Setup of {@link ISideNavigationCallback} for callback of item click.
  95
+	 * @param callback
  96
+	 */
86 97
 	public void setMenuClickCallback(ISideNavigationCallback callback) {
87 98
 		this.callback = callback;
88 99
 	}
89 100
 
  101
+	/**
  102
+	 * Setup of side menu items.
  103
+	 * @param menu - resource ID
  104
+	 */
90 105
 	public void setMenuItems(int menu) {
91 106
 		parseXml(menu);
92 107
 		if (menuItems != null && menuItems.size() > 0) {
93  
-			listView.setAdapter(new Adapter());
  108
+			listView.setAdapter(new SideNavigationAdapter());
94 109
 		}
95 110
 	}
96 111
 
  112
+	/**
  113
+	 * 
  114
+	 */
97 115
 	public void setBackgroundResource(int resource) {
98 116
 		listView.setBackgroundResource(resource);
99 117
 	}
100 118
 
  119
+	/**
  120
+	 * Show side navigation menu.
  121
+	 */
101 122
 	public void showMenu() {
102 123
 		outsideView.setVisibility(View.VISIBLE);
103 124
 		outsideView.startAnimation(AnimationUtils.loadAnimation(getContext(),
@@ -107,6 +128,9 @@ public void showMenu() {
107 128
 				R.anim.side_navigation_in_from_left));
108 129
 	}
109 130
 
  131
+	/**
  132
+	 * Hide side navigation menu.
  133
+	 */
110 134
 	public void hideMenu() {
111 135
 		outsideView.setVisibility(View.GONE);
112 136
 		outsideView.startAnimation(AnimationUtils.loadAnimation(getContext(),
@@ -116,16 +140,28 @@ public void hideMenu() {
116 140
 				R.anim.side_navigation_out_to_left));
117 141
 	}
118 142
 
  143
+	/**
  144
+	 * Show/Hide side navigation menu depending on visibility.
  145
+	 */
119 146
 	public void toggleMenu() {
120  
-		if (outsideView.getVisibility() == View.GONE) {
121  
-			showMenu();
122  
-		} else {
  147
+		if (isShown()) {
123 148
 			hideMenu();
  149
+		} else {
  150
+			showMenu();
124 151
 		}
125 152
 	}
  153
+	
  154
+	@Override
  155
+	public boolean isShown() {
  156
+		return navigationMenu.isShown();
  157
+	}
126 158
 
  159
+	/**
  160
+	 * Parse XML describe menu.
  161
+	 * @param menu - resource ID
  162
+	 */
127 163
 	private void parseXml(int menu) {
128  
-		menuItems = new ArrayList<SideNavigationView.SideNavigationItem>();
  164
+		menuItems = new ArrayList<SideNavigationItem>();
129 165
 		try {
130 166
 			XmlResourceParser xrp = getResources().getXml(menu);
131 167
 			xrp.next();
@@ -144,9 +180,9 @@ private void parseXml(int menu) {
144 180
 								"http://schemas.android.com/apk/res/android",
145 181
 								"id");
146 182
 						SideNavigationItem item = new SideNavigationItem();
147  
-						item.id = Integer.valueOf(resId.replace("@", ""));
148  
-						item.text = resourceIdToString(textId);
149  
-						item.icon = Integer.valueOf(iconId.replace("@", ""));
  183
+						item.setId(Integer.valueOf(resId.replace("@", "")));
  184
+						item.setText(resourceIdToString(textId));
  185
+						item.setIcon(Integer.valueOf(iconId.replace("@", "")));
150 186
 						menuItems.add(item);
151 187
 					}
152 188
 				}
@@ -157,25 +193,24 @@ private void parseXml(int menu) {
157 193
 		}
158 194
 	}
159 195
 
160  
-	private String resourceIdToString(String text) {
161  
-		if (!text.contains("@")) {
162  
-			return text;
  196
+	/**
  197
+	 * Convert resource ID to String.
  198
+	 * @param text
  199
+	 * @return
  200
+	 */
  201
+	private String resourceIdToString(String resId) {
  202
+		if (!resId.contains("@")) {
  203
+			return resId;
163 204
 		} else {
164  
-			String id = text.replace("@", "");
  205
+			String id = resId.replace("@", "");
165 206
 			return getResources().getString(Integer.valueOf(id));
166 207
 		}
167 208
 	}
168 209
 
169  
-	class SideNavigationItem {
170  
-		int id;
171  
-		String text;
172  
-		int icon;
173  
-	}
174  
-
175  
-	private class Adapter extends BaseAdapter {
  210
+	private class SideNavigationAdapter extends BaseAdapter {
176 211
 		private LayoutInflater inflater;
177 212
 
178  
-		public Adapter() {
  213
+		public SideNavigationAdapter() {
179 214
 			inflater = LayoutInflater.from(getContext());
180 215
 		}
181 216
 
@@ -200,16 +235,14 @@ public View getView(int position, View convertView, ViewGroup parent) {
200 235
 			if (convertView == null) {
201 236
 				convertView = inflater.inflate(R.layout.side_navigation_item, null);
202 237
 				holder = new ViewHolder();
203  
-				holder.image = (ImageView) convertView
204  
-						.findViewById(R.id.side_navigation_item_icon);
205  
-				holder.text = (TextView) convertView
206  
-						.findViewById(R.id.side_navigation_item_text);
  238
+				holder.image = (ImageView) convertView.findViewById(R.id.side_navigation_item_icon);
  239
+				holder.text = (TextView) convertView.findViewById(R.id.side_navigation_item_text);
207 240
 				convertView.setTag(holder);
208 241
 			} else {
209 242
 				holder = (ViewHolder) convertView.getTag();
210 243
 			}
211  
-			holder.image.setImageResource(menuItems.get(position).icon);
212  
-			holder.text.setText(menuItems.get(position).text);
  244
+			holder.image.setImageResource(menuItems.get(position).getIcon());
  245
+			holder.text.setText(menuItems.get(position).getText());
213 246
 			return convertView;
214 247
 		}
215 248
 
10  sample/src/com/devspark/sidenavigation/sample/MainActivity.java
@@ -78,5 +78,15 @@ public void onSideNavigationItemClick(int itemId) {
78 78
 		}
79 79
 
80 80
 	}
  81
+	
  82
+	@Override
  83
+	public void onBackPressed() {
  84
+		// hide menu if it shown
  85
+		if (sideNavigationView.isShown()) {
  86
+			sideNavigationView.hideMenu();
  87
+		} else {
  88
+			super.onBackPressed();
  89
+		}
  90
+	}
81 91
 
82 92
 }

0 notes on commit 5d26c22

Please sign in to comment.
Something went wrong with that request. Please try again.