我目前正在使用material drawer library by Mike Penz。到目前为止,我有一个布局和几乎功能齐全的“基本应用程序”,包括几个片段和导航抽屉。
我正在尝试添加根据用户是否已登录而更改的粘性页脚项。虽然我在用户注销项目更新时有一些预期的效果,但仅在重新启动应用时显示,反之亦然。登录。
正如您将在下面的代码中看到的,我的最后一次尝试是删除登录,然后添加注销,反之亦然,但菜单在重新启动之前不会更改。
我觉得我错过了一些简单的东西,但我在迈克的网站上找不到的任何东西都有帮助。其他人可以帮忙吗?
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import com.mikepenz.fontawesome_typeface_library.FontAwesome;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;
public class MainActivity extends AppCompatActivity {
private SharedPreferences pref;
private static final int PROFILE_SETTING = 1;
//save our header or result
private AccountHeader headerResult = null;
private Drawer result = null;
private IProfile profile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);
goToHome();
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.drawer_item_home);
// Create a few sample profile
profile = new ProfileDrawerItem().withName("Mike Penz").withEmail("mikepenz@gmail.com").withIcon(getResources().getDrawable(R.drawable.profile));
// Create the AccountHeader
buildHeader(savedInstanceState);
//Create the drawer
result = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.drawer_item_home).withIcon(FontAwesome.Icon.faw_home).withIdentifier(1).withIdentifier(10).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_find).withIcon(FontAwesome.Icon.faw_search).withIdentifier(2).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_deals).withIcon(FontAwesome.Icon.faw_tag).withIdentifier(3).withSelectable(false)
) // add the items we want to use with our Drawer
.withOnDrawerNavigationListener(new Drawer.OnDrawerNavigationListener() {
@Override
public boolean onNavigationClickListener(View clickedView) {
//this method is only called if the Arrow icon is shown. The hamburger is automatically managed by the MaterialDrawer
//if the back arrow is shown. close the activity
MainActivity.this.finish();
//return true if we have consumed the event
return true;
}
})
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if (drawerItem.equals(1)) {
// Home
goToHome();
} else if (drawerItem.equals(2)) {
// Find
goToFind();
} else if (drawerItem.equals(3)) {
// Deals
goToDeals();
} else if (drawerItem.equals(97)) {
// Settings
goToSettings();
} else if (drawerItem.equals(98)) {
// Sign In
goToLogin();
} else if (drawerItem.equals(99)) {
// Invite
goToLogout();
}
return false;
}
})
.withSavedInstance(savedInstanceState)
.build();
if (pref.getBoolean(Constants.IS_LOGGED_IN, false)) {
result.addStickyFooterItem(new PrimaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog).withIdentifier(97).withSelectable(false));
result.removeStickyFooterItemAtPosition(98);
result.addStickyFooterItem(new PrimaryDrawerItem().withName(R.string.drawer_item_logout).withIcon(FontAwesome.Icon.faw_sign_out).withIdentifier(99).withSelectable(false));
} else {
result.addStickyFooterItem(new PrimaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog).withIdentifier(97).withSelectable(false));
result.removeStickyFooterItemAtPosition(99);
result.addStickyFooterItem(new PrimaryDrawerItem().withName(R.string.drawer_item_login).withIcon(FontAwesome.Icon.faw_sign_in).withIdentifier(98).withSelectable(false));
}
}
/**
* small helper method to reuse the logic to build the AccountHeader
* this will be used to replace the header of the drawer with a compact/normal header
*
* @param savedInstanceState
*/
private void buildHeader(Bundle savedInstanceState) {
// Create the AccountHeader
headerResult = new AccountHeaderBuilder()
.withSelectionListEnabledForSingleProfile(false)
.withActivity(this)
.withHeaderBackground(R.drawable.header)
.withCompactStyle(true)
.addProfiles(
profile
)
.withSavedInstance(savedInstanceState)
.build();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
//add the values which need to be saved from the drawer to the bundle
outState = result.saveInstanceState(outState);
//add the values which need to be saved from the accountHeader to the bundle
outState = headerResult.saveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@Override
public void onBackPressed() {
//handle the back press :D close the drawer first and if the drawer is closed close the activity
if (result != null && result.isDrawerOpen()) {
result.closeDrawer();
} else {
super.onBackPressed();
}
}
private void initFragment() {
Fragment fragment;
if (pref.getBoolean(Constants.IS_LOGGED_IN, false)) {
fragment = new HomeFragment();
} else {
fragment = new LoginFragment();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, fragment);
ft.commit();
}
private void goToHome() {
Fragment home = new HomeFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, home);
ft.commit();
}
private void goToFind() {
Fragment find = new FindFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, find);
ft.commit();
}
private void goToDeals() {
Fragment deals = new DealsFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, deals);
ft.commit();
}
private void goToSettings() {
Fragment settings = new SettingsFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, settings);
ft.commit();
}
private void goToLogin() {
Fragment login = new LoginFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, login);
ft.commit();
}
private void goToLogout() {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.IS_LOGGED_IN, false);
editor.putString(Constants.EMAIL, "");
editor.putString(Constants.NAME, "");
editor.putString(Constants.UNIQUE_ID, "");
editor.apply();
goToHome();
}
}
答案 0 :(得分:0)
onCreate。尝试在goToLogout中更改抽屉
private void refreshDrawer() {
if(pref.getBoolean(Constants.IS_LOGGED_IN,false)){
result.removeStickyFooterItemAtPosition(..);
result.addStickyFooterItem(..);
}else {
result.removeStickyFooterItemAtPosition(..);
result.addStickyFooterItem(..);
}
}
private void goToLogout(){
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.IS_LOGGED_IN,false);
editor.putString(Constants.EMAIL,"");
editor.putString(Constants.NAME,"");
editor.putString(Constants.UNIQUE_ID,"");
editor.commit();
....
refreshDrawer()
}