我有这个导航抽屉。当应用程序打开时,它工作正常,它不会显示。但是当我通过它导航到一个菜单然后按回来时,它不应该出现,但确实如此。我怎样才能解决这个问题。谢谢!
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class NavigationDrawer extends Fragment implements NavDrawerRVAdapter.ClickListener {
private RecyclerView recyclerView;
public static final String KEY_USER_LEARNED_DRAWER = "user_learned_drawer";
private View containerView;
public static final String PREF_FILE_NAME = "testpref";
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
private NavDrawerRVAdapter adapter;
public NavigationDrawer() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new NavDrawerRVAdapter(getActivity(), getData());
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
// Oncreate & Set up for navdrawer
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// mUserLearnedDrawer =readFromPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,true);
mUserLearnedDrawer =true;
if (savedInstanceState != null)
mFromSavedInstanceState = true;
}
public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
containerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!mUserLearnedDrawer) {
mUserLearnedDrawer = true;
SaveToPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, true );
}
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
toolbar.setAlpha(1 - slideOffset / 2);
}
};
if (!mUserLearnedDrawer&&!mFromSavedInstanceState) {
mDrawerLayout.openDrawer(containerView);
}
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public static void SaveToPreferences(Context context, String preferenceName, boolean preferenceValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(preferenceName, preferenceValue);
editor.apply();
}
public static boolean readFromPreferences(Context context, String preferenceName, boolean defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(preferenceName, defaultValue);
}
@Override
public void itemClicked(View view, int position) {
switch (position) {
case 1:
startActivity(new Intent(getActivity(), AccountsAndCards.class));
break;
default:
startActivity(new Intent(getActivity(), AccountsAndCards.class));
}
}
}
答案 0 :(得分:1)
在您的抽屉itemClicked
方法中关闭抽屉。
mDrawerLayout.closeDrawers();