android 抽屉关闭时的卡顿
2016-06-12 00:00
731 查看
摘要: android drawer close avoid stuttering
1.from https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
2.具体实现
http://stackoverflow.com/questions/18343018/optimizing-drawer-and-activity-launching-speed
Avoid performing expensive operations such as layout during animation as it can cause stuttering; try to perform expensive operations during the STATE_IDLE state.
Instead of using a
Inside MainActivity,
Set the
Finally,
1.from https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
DrawerLayout.DrawerListenercan be used to monitor the state and motion of drawer views. Avoid performing expensive operations such as layout during animation as it can cause stuttering; try to perform expensive operations during the
STATE_IDLEstate.
DrawerLayout.SimpleDrawerListeneroffers default/no-op implementations of each callback method.
2.具体实现
http://stackoverflow.com/questions/18343018/optimizing-drawer-and-activity-launching-speed
Avoid performing expensive operations such as layout during animation as it can cause stuttering; try to perform expensive operations during the STATE_IDLE state.
Instead of using a
Handlerand hard-coding the time delay, you can override the
onDrawerStateChangedmethod of
ActionBarDrawerToggle(which implements
DrawerLayout.DrawerListener), so that you can perform the expensive operations when the drawer is fully closed.
Inside MainActivity,
private class SmoothActionBarDrawerToggle extends ActionBarDrawerToggle { private Runnable runnable; public SmoothActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) { super(activity, drawerLayout, toolbar, openDrawerContentDescRes, closeDrawerContentDescRes); } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); invalidateOptionsMenu(); } @Override public void onDrawerClosed(View view) { super.onDrawerClosed(view); invalidateOptionsMenu(); } @Override public void onDrawerStateChanged(int newState) { super.onDrawerStateChanged(newState); if (runnable != null && newState == DrawerLayout.STATE_IDLE) { runnable.run(); runnable = null; } } public void runWhenIdle(Runnable runnable) { this.runnable = runnable; } }
Set the
DrawerListenerin
onCreate:
mDrawerToggle = new SmoothActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open, R.string.close); mDrawerLayout.setDrawerListener(mDrawerToggle);
Finally,
private void selectItem(int position) { switch (position) { case DRAWER_ITEM_SETTINGS: { mDrawerToggle.runWhenIdle(new Runnable() { @Override public void run() { Intent intent = new Intent(MainActivity.this, SettingsActivity.class); startActivity(intent); } }); mDrawerLayout.closeDrawers(); break; } case DRAWER_ITEM_HELP: { mDrawerToggle.runWhenIdle(new Runnable() { @Override public void run() { Intent intent = new Intent(MainActivity.this, HelpActivity.class); startActivity(intent); } }); mDrawerLayout.closeDrawers(); break; } } }
相关文章推荐
- Android手柄焦点控制
- Android地图应用开发之--百度地图集成及应用_baidumap
- 2015-2016最火的Android开源项目--github开源项目集锦(不看你就out了)
- Android消息机制原理剖析—Handler篇
- Android——登录界面、SharedPreferences实现记住密码等账户信息
- Android之onWindowFocusChanged
- android studio 重新运行不了的问题
- android四大组件—Activity
- Android学习路线图
- Android PreferenceFragment实现设置界面
- android--1--SDK命令行搭建并运行Android
- Android Intent(一)
- Android蓝牙BLE(二)--使用蓝牙主机
- android关于屏幕适配的几点建议
- Android Fragment 的使用
- Android 闹钟设置详解
- Android四大组件--Broadcast Receiver
- Retrofit初体验
- ViewPager 详解(一)---基本入门
- Android初级教程三个Dialog对话框小案例