toolbar 和 drawer 搭配

1.先看看效果

2.定义style.xml

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
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
<item name="vpiTabPageIndicatorStyle">@style/TinderLiker.IndicatorStyle</item>
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<!-- Actionbar color -->
<item name="colorPrimary">@color/red</item>
<!--Status bar color-->
<item name="colorPrimaryDark">@color/red</item>
<!--Window color-->
<item name="android:windowBackground">@android:color/white</item>
</style>
<style name="TinderLiker.IndicatorStyle" parent="@style/Widget.TabPageIndicator">
<item name="android:background">@drawable/custom_tab_indicator</item>
<item name="android:divider">@drawable/custom_tab_indicator_divider</item>
<item name="android:gravity">center</item>
<item name="android:textColor">@drawable/text_color_viewpager_tab</item>
<item name="android:textSize">18sp</item>
<item name="android:textStyle">normal</item>
</style>
</resources>

3.定义toolbar.xml 的布局视图

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>

4.定义activity_main.xml 的布局视图

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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
layout="@layout/toolbar"
android:id="@+id/toolbar" />
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar">
<!--
As the main content view, the view below consumes the entire
space available using match_parent in both dimensions.
-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--
android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view.
-->
<ListView
android:id="@+id/left_drawer"
android:layout_width="285dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

5.MainActivity.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package com.ilegendsoft.toprankapps.tinderliker.activities;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.ilegendsoft.ilsalib.ilsautils.IlsUtilsManager;
import com.ilegendsoft.ilsalib.ilsautils.log.Logger;
import com.ilegendsoft.ilsalib.ilsazcloud.item.CloudyParameter;
import com.ilegendsoft.ilsalib.ilsazcloud.utils.ZCloudAccountUtils;
import com.ilegendsoft.ilsalib.ilsazcloud.utils.ZCloudCloudyParameterUtil;
import com.ilegendsoft.toprankapps.tinderliker.R;
import com.ilegendsoft.toprankapps.tinderliker.adapters.DrawerAdapter;
import com.ilegendsoft.toprankapps.tinderliker.fragments.DiscoverFragment;
import com.ilegendsoft.toprankapps.tinderliker.fragments.LocationFragment;
import com.ilegendsoft.toprankapps.tinderliker.fragments.SettingsFragment;
import com.ilegendsoft.toprankapps.tinderliker.fragments.TinderLikerFragment;
import com.ilegendsoft.toprankapps.tinderliker.fragments.TindersFragment;
import com.ilegendsoft.toprankapps.tinderliker.items.Drawer;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class MainActivity extends BaseActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBar mActionBar;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerAdapter mDrawerAdapter;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private ArrayList<Drawer> mDrawers;
private FragmentManager mFragmentManager;
private TinderLikerFragment mTinderLikerFragment;
private DiscoverFragment mDiscoverFragment;
private TindersFragment mTindersFragment;
private LocationFragment mLocationFragment;
private SettingsFragment mSettingsFragment;
private long mLastBackPressedTime = 0;
private Fragment mCurrentFragment;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
if (savedInstanceState == null) {
selectItem(0);
}
checkCloudyParameter();
}
private void checkCloudyParameter() {
ZCloudCloudyParameterUtil.getCloudyParameterRequest(Volley.newRequestQueue(this),
ZCloudAccountUtils.getAccessTokenNoLogin(this), this,
new ZCloudCloudyParameterUtil.OnSuccessListener() {
@Override
public void onSuccess(CloudyParameter cloudyParameter) {
IlsUtilsManager.getInstance().check(MainActivity.this,
cloudyParameter.getAlternative(), cloudyParameter.getVersion(), 0, null);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
IlsUtilsManager.getInstance().check(MainActivity.this, "", "", 0, null);
Logger.toast(MainActivity.this, R.string.network_error);
}
});
}
private void init() {
mTitle = mDrawerTitle = getTitle();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mActionBar = getSupportActionBar();
// enable ActionBar app icon to behave as action to toggle nav drawer
//mActionBar.setDisplayHomeAsUpEnabled(true);
//mActionBar.setHomeButtonEnabled(true);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
initDrawerListData();
mDrawerAdapter = new DrawerAdapter(this, mDrawers);
mDrawerList.setAdapter(mDrawerAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
mToolbar, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
mActionBar.setTitle(mTitle);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
mActionBar.setTitle(mDrawerTitle);
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
initMainFragment();
}
private void initMainFragment() {
mFragmentManager = getSupportFragmentManager();
mTinderLikerFragment = TinderLikerFragment.newInstance();
mDiscoverFragment = DiscoverFragment.newInstance();
mTindersFragment = TindersFragment.newInstance();
mLocationFragment = LocationFragment.newInstance();
mSettingsFragment = SettingsFragment.newInstance();
}
private void initDrawerListData() {
mDrawers = new ArrayList<Drawer>();
String[] drawerNames = getResources().getStringArray(
R.array.drawer_array);
for (int i = 0; i < drawerNames.length; i++) {
Drawer drawer = new Drawer();
switch (i) {
case 0:
drawer.setImageResourceId(R.drawable.btn_navgationdrawer_tinderlike);
break;
case 1:
drawer.setImageResourceId(R.drawable.btn_navgationdrawer_tinders);
break;
case 2:
drawer.setImageResourceId(R.drawable.btn_navgationdrawer_discoversetting);
break;
case 3:
drawer.setImageResourceId(R.drawable.btn_navgationdrawer_locationsetting);
break;
case 4:
drawer.setImageResourceId(R.drawable.btn_navgationdrawer_setting);
break;
default:
break;
}
drawer.setName(drawerNames[i]);
mDrawers.add(drawer);
}
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content
// view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
if (drawerOpen) hideSoftKeyBoard();
if (mCurrentFragment == mTinderLikerFragment) {
int[] ids = {R.id.action_auto};
controlMenuDisplay(menu, drawerOpen, ids);
} else if (mCurrentFragment == mLocationFragment) {
int[] ids = {R.id.action_search};
controlMenuDisplay(menu, drawerOpen, ids);
}
return super.onPrepareOptionsMenu(menu);
}
@Override
protected void onResume() {
super.onResume();
}
private void hideSoftKeyBoard() {
InputMethodManager inputManager = (InputMethodManager) this
.getSystemService(Context.INPUT_METHOD_SERVICE);
// check if no view has focus:
View view = this.getCurrentFocus();
if (view == null)
return;
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
private void controlMenuDisplay(Menu menu, boolean drawerOpen, int[] ids) {
for (int id : ids) {
MenuItem item = menu.findItem(id);
if (item != null) {
item.setVisible(!drawerOpen);
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
}
}
private void selectItem(int position) {
switch (position) {
case 0:
mCurrentFragment = mTinderLikerFragment;
break;
case 1:
mCurrentFragment = mTindersFragment;
break;
case 2:
mCurrentFragment = mDiscoverFragment;
break;
case 3:
mCurrentFragment = mLocationFragment;
break;
case 4:
mCurrentFragment = mSettingsFragment;
break;
default:
break;
}
mFragmentManager.beginTransaction()
.replace(R.id.content_frame, mCurrentFragment).commit();
// update selected item and title, then close the drawer
setTitle(mDrawers.get(position).getName());
// handle the not smooth drawer.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mDrawerLayout.closeDrawer(mDrawerList);
}
}, 200);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
TextView titleView = getToolBarTitleTextView();
if (titleView != null) {
Typeface font = Typeface.createFromAsset(getAssets(), "SansitaOne.ttf");
titleView.setTypeface(font);
titleView.setText(mTitle);
titleView.setTextSize(23);
titleView.setTextColor(0xDDFFFFFF);
} else {
mActionBar.setTitle(mTitle);
}
}
private TextView getToolBarTitleTextView() {
TextView titleTextView = null;
try {
Field f = mToolbar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
titleTextView = (TextView) f.get(mToolbar);
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
}
return titleTextView;
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed() {
if (System.currentTimeMillis() - mLastBackPressedTime < 2000) {
super.onBackPressed();
} else {
mLastBackPressedTime = System.currentTimeMillis();
Logger.toast(this, R.string.exit);
}
}
}