-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathSlidingActivityHelper.java
222 lines (193 loc) · 6.41 KB
/
SlidingActivityHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.slidingmenu.lib.app;
import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import com.slidingmenu.lib.R;
import com.slidingmenu.lib.SlidingMenu;
public class SlidingActivityHelper {
private Activity mActivity;
private SlidingMenu mSlidingMenu;
private View mViewAbove;
private View mViewBehind;
private boolean mBroadcasting = false;
private boolean mOnPostCreateCalled = false;
private boolean mEnableSlide = false;
/**
* Instantiates a new SlidingActivityHelper.
*
* @param activity the associated activity
*/
public SlidingActivityHelper(Activity activity) {
mActivity = activity;
}
/**
* Sets mSlidingMenu as a newly inflated SlidingMenu. Should be called within the activitiy's onCreate()
*
* @param savedInstanceState the saved instance state (unused)
*/
public void onCreate(Bundle savedInstanceState) {
mSlidingMenu = (SlidingMenu) LayoutInflater.from(mActivity).inflate(R.layout.slidingmenumain, null);
}
/**
* Further SlidingMenu initialization. Should be called within the activitiy's onPostCreate()
*
* @param savedInstanceState the saved instance state (unused)
*/
public void onPostCreate(Bundle savedInstanceState) {
if (mViewBehind == null || mViewAbove == null) {
throw new IllegalStateException("Both setBehindContentView must be called " +
"in onCreate in addition to setContentView.");
}
mOnPostCreateCalled = true;
// get the window background
TypedArray a = mActivity.getTheme().obtainStyledAttributes(new int[] {android.R.attr.windowBackground});
int background = a.getResourceId(0, 0);
a.recycle();
if (mEnableSlide) {
// move everything into the SlidingMenu
ViewGroup decor = (ViewGroup) mActivity.getWindow().getDecorView();
ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
// save ActionBar themes that have transparent assets
decorChild.setBackgroundResource(background);
decor.removeView(decorChild);
mSlidingMenu.setContent(decorChild);
decor.addView(mSlidingMenu);
} else {
// take the above view out of
ViewGroup parent = (ViewGroup) mViewAbove.getParent();
if (parent != null) {
parent.removeView(mViewAbove);
}
// save people from having transparent backgrounds
if (mViewAbove.getBackground() == null) {
mViewAbove.setBackgroundResource(background);
}
mSlidingMenu.setContent(mViewAbove);
parent.addView(mSlidingMenu, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
this.showContent();
}
/**
* Controls whether the ActionBar slides along with the above view when the menu is opened,
* or if it stays in place.
*
* @param slidingActionBarEnabled True if you want the ActionBar to slide along with the SlidingMenu,
* false if you want the ActionBar to stay in place. According to the Android Design guidelines, this should be false.
*/
public void setSlidingActionBarEnabled(boolean slidingActionBarEnabled) {
if (mOnPostCreateCalled)
throw new IllegalStateException("enableSlidingActionBar must be called in onCreate.");
mEnableSlide = slidingActionBarEnabled;
}
/**
* Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
*
* @param id the resource id of the desired view
* @return The view if found or null otherwise.
*/
public View findViewById(int id) {
View v;
if (mSlidingMenu != null) {
v = mSlidingMenu.findViewById(id);
if (v != null)
return v;
}
return null;
}
/**
* Called to retrieve per-instance state from an activity before being killed so that the state can be
* restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method
* will be passed to both).
*
* @param outState Bundle in which to place your saved state.
*/
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean("menuOpen", mSlidingMenu.isMenuShowing());
}
/**
* Register the above content view.
*
* @param v the above content view to register
* @param params LayoutParams for that view (unused)
*/
public void registerAboveContentView(View v, LayoutParams params) {
if (!mBroadcasting)
mViewAbove = v;
}
/**
* Set the activity content to an explicit view. This view is placed directly into the activity's view
* hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters
* of the specified view are ignored. Both the width and the height of the view are set by default to
* MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View,
* android.view.ViewGroup.LayoutParams) instead.
*
* @param v The desired content to display.
*/
public void setContentView(View v) {
mBroadcasting = true;
mActivity.setContentView(v);
}
/**
* Set the behind view content to an explicit view. This view is placed directly into the behind view 's view hierarchy.
* It can itself be a complex view hierarchy.
*
* @param view The desired content to display.
* @param layoutParams Layout parameters for the view. (unused)
*/
public void setBehindContentView(View view, LayoutParams layoutParams) {
mViewBehind = view;
mSlidingMenu.setMenu(mViewBehind);
}
/**
* Gets the SlidingMenu associated with this activity.
*
* @return the SlidingMenu associated with this activity.
*/
public SlidingMenu getSlidingMenu() {
return mSlidingMenu;
}
/**
* Toggle the SlidingMenu. If it is open, it will be closed, and vice versa.
*/
public void toggle() {
mSlidingMenu.toggle();
}
/**
* Close the SlidingMenu and show the content view.
*/
public void showContent() {
mSlidingMenu.showContent();
}
/**
* Open the SlidingMenu and show the menu view.
*/
public void showMenu() {
mSlidingMenu.showMenu();
}
/**
* Open the SlidingMenu and show the secondary menu view. Will default to the regular menu
* if there is only one.
*/
public void showSecondaryMenu() {
mSlidingMenu.showSecondaryMenu();
}
/**
* On key up.
*
* @param keyCode the key code
* @param event the event
* @return true, if successful
*/
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && mSlidingMenu.isMenuShowing()) {
showContent();
return true;
}
return false;
}
}