@benjosantony建議你應該在onResume
打開你的抽屜裏,但它不能保證該活動將是可見當時:
的onResume是不是你 活動是可見的最佳指標用戶;使用onWindowFocusChanged(布爾)肯定知道 你的活動是給用戶
你會認爲你可以只使用onWindowFocusChanged
和做可見的,但你不能。目前仍然打破(至少對我來說)抽屜的動畫過渡動畫..
對於API 21+:
有onEnterAnimationComplete
在那裏你可以打開你的抽屜,看到動畫正常。然而21+是,這只是太大的要求..
對於較低的API:
我能想到的唯一可能的途徑是與主題調整除去活動的動畫:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowAnimationStyle">@null</item>
</style>
而且打開抽屜,像這樣:
private static final String DRAWER_STATE = "mDrawerOpened";
private DrawerLayout mDrawer;
private ListView mDrawerList;
private boolean mDrawerOpened;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!mDrawerOpened && hasFocus) {
mDrawer.openDrawer(mDrawerList);
mDrawerOpened = true;
}
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(DRAWER_STATE, mDrawerOpened);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mDrawerOpened = savedInstanceState.getBoolean(DRAWER_STATE);
}
這將動畫只有當活動開始的抽屜。
當你的活動異常破壞布爾值被保存,例如輪換或需要系統資源。
如果您不喜歡設置instanceState,您可以使用SharedPreferences
作爲@Biu的建議,但IMO不會是正確的解決方案,因爲android已經提供了相應的工具,因此無需重新發明自行車。
Android的用戶知道滑動菜單,它是已知的UI模式。所以你不需要在你的應用程序中實現這種行爲。 – gio 2014-12-27 16:05:03
這就是我的想法。無論如何,我想知道什麼時候調用任何東西,以便在用戶啓動應用程序並且用戶界面(至少部分)加載時啓動它。 – natario 2014-12-27 16:07:48
使用處理程序併發布調用opendrawer()的runnable。 – ElDuderino 2014-12-27 23:02:14