NavigationDrawer按钮不起作用

时间:2016-07-14 10:13:27

标签: android

我设置了NavigationDrawer,但NavigationDrawer按钮无效。

NavigationDrawerFragment.java

public class NavigationDrawerFragment extends Fragment {


public static final String PREF_FILE_NAME="testpref";
public static final String USER_LEARNED_DRAWER="user_learned_drawer";

public ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private View containerView;

//for disapper drawer when screen rotate

private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
public NavigationDrawerFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mUserLearnedDrawer=Boolean.valueOf(readFromPreference(getActivity(),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_drawer, container, false);
}

public void setUp(int fragmentId,DrawerLayout drawerlayout) {

   containerView=getActivity().findViewById(fragmentId);
    mDrawerLayout=drawerlayout;
    mDrawerToggle=new ActionBarDrawerToggle(getActivity(),drawerlayout,R.string.drawer_open,R.string.drawer_close){

        @Override
        public void onDrawerOpened(View drawerView) {
            if (!mUserLearnedDrawer){
                mUserLearnedDrawer=true;
                saveToPreference(getActivity(),PREF_FILE_NAME,mUserLearnedDrawer+"");
            }
            getActivity().invalidateOptionsMenu();
            super.onDrawerOpened(drawerView);
        }

        @Override
        public void onDrawerClosed(View drawerView) {

            getActivity().invalidateOptionsMenu();
            super.onDrawerClosed(drawerView);
        }
    };
    if (!mUserLearnedDrawer&& !mFromSavedInstanceState){
       mDrawerLayout.openDrawer(containerView);
    }

    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerLayout.post(new Runnable() {
       @Override
       public void run() {

           mDrawerToggle.syncState();
       }
    });
}

public static void saveToPreference(Context context,String preferenceName,String preferenceValue){
     SharedPreferences sharedPreferences=context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE);
     SharedPreferences.Editor editor=sharedPreferences.edit();
     editor.putString(preferenceName,preferenceValue);
     editor.apply();
}
public static String readFromPreference(Context context, String preferenceName, String defaultValue){
    SharedPreferences sharedPreferences=context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE);
    return sharedPreferences.getString(preferenceName,defaultValue);
}
}

MainActivity.java:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
   getSupportActionBar().setHomeButtonEnabled(true);

    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation);
    drawerFragment.setUp(R.id.fragment_navigation,(DrawerLayout) findViewById(R.id.drawer_layout));

}
}

1 个答案:

答案 0 :(得分:1)

在setUp方法的最后一行添加以下代码行

    mDrawerLayout.addDrawerListener(mDrawerToggle);