所以我正在尝试的是如果DrawerLayout是打开的,而且我旋转屏幕,DrawerLayout应该关闭,但它不会
这是代码
package com.moammedx.learingmaterial;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
public class NavigationDrwaerFragment extends Fragment {
public static final String PREF_FILE_NAME = "testpref";
public static final String KEY_USER_LEARNED_DRAWER = "user_learnd_drawer";
private View contenerView;
private ActionBarDrawerToggle mDrawerTolggle;
private DrawerLayout mDrawerLayout;
private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
public NavigationDrwaerFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, "false"));
if (savedInstanceState != null) {
mFromSavedInstanceState = true;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_navigation_drwaer, container, false);
}
public void SetUp(int fragmenID, DrawerLayout drawerlayout, Toolbar toolbar) {
contenerView = getActivity().findViewById(fragmenID);
mDrawerLayout = drawerlayout;
mDrawerTolggle = new ActionBarDrawerToggle(getActivity(), drawerlayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
if (!mUserLearnedDrawer) {
super.onDrawerOpened(drawerView);
mUserLearnedDrawer = true;
saveToPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, mUserLearnedDrawer + "");
}
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
Toast.makeText(getActivity(), "onDrawerClosed", Toast.LENGTH_SHORT).show();
}
};
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(contenerView);
}
if (mFromSavedInstanceState) {
Toast.makeText(getActivity(), "close now", Toast.LENGTH_SHORT).show();
mDrawerLayout.closeDrawer(contenerView);
}
mDrawerLayout.setDrawerListener(mDrawerTolggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerTolggle.syncState();
}
});
}
public static void saveToPreferences(Context context, String preferencesName, String preferencesValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(preferencesName, preferencesValue);
editor.apply();
}
public static String readFromPreferences(Context context, String preferencesName, String defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(preferencesName, defaultValue);
}
}
从主活动
调用SetUp方法在onCreat中,我检查了savedInstanceState是否存在 然后在SetUp中使用.colseDrawer来关闭DrawerLayout,但它没有关闭,我为吐司添加了仅用于测试,它在我旋转屏幕时显示
答案 0 :(得分:0)
我是否可以通过在onResume
中添加.closeDrawer来解决这个问题{{1}}
答案 1 :(得分:0)
原因是因为DrawerLayout close命令的内部工作原理未包含在post runnable中。意思是,你的关闭命令会触发,但在它的状态保存开始之前并将其返回到它之前的状态(在这种情况下是打开)。要修复代码,只需将close命令的内容包装在post runnable中。它将在调用onCreate()和onRestoreState()之后执行,在旋转时关闭抽屉。