简体   繁体   English

一个会话结束后如何在导航抽屉中隐藏项目

[英]how to hide the item in Navigation drawer after one session is over

I have a NavigationDrawer in my Activity with three items. 我的Activity有一个包含三个项目的NavigationDrawer I want to show all three items when first time user Login. 我想在首次登录时显示所有三个项目。 In other session I want to make one item invisible and show only two items in NavigationDrawer . 在其他会话中,我想使一项不可见,并在NavigationDrawer仅显示两项。

you have to detect first launch of the app using this code 您必须使用此代码来检测应用程序的首次启动

public class MyActivity extends Activity {

SharedPreferences prefs = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Perhaps set content view here

    prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
}

@Override
protected void onResume() {
    super.onResume();

    if (prefs.getBoolean("firstrun", true)) {
        // Do first run stuff here then set 'firstrun' as false
        // using the following line to edit/commit prefs
        prefs.edit().putBoolean("firstrun", false).commit();
    }
}
}

add 1 item to navigation drawer at first launch or after first launch is completed, remove your 1 of the item from navigation drawer 在首次启动时或在首次启动完成后将1个项目添加到导航抽屉中,从导航抽屉中删除其中1个项目

USE SharedPreferences to store user status! 使用SharedPreferences存储用户状态!

public class SharedPrefModel {
    public static String INFO_STORE_TAG = "user_info";

    public static String sharedPrefName = "USER";
    private SharedPreferences sharedPref;

    public SharedPrefModel(Context context) {
        this.sharedPref = context.getSharedPreferences(sharedPrefName, MODE_PRIVATE);

    }

    public void setStatus(Boolean isFirstTime) {

        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(INFO_STORE_TAG, isFirstTime);
        editor.apply();
    }
    public Boolean getStatus() {
        return sharedPref.getBoolean(INFO_STORE_TAG,false);

    }


    public void clearInfo() {
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.clear();
        editor.apply();
    }
}

After login for the first time set the status to false. 首次登录后,将状态设置为false。

new SharedPrefModel(this).setStatus(false);

Next Time check that if the status is true or not. 下次检查状态是否为true。

if(!new SharedPrefModel(this).getStatus()){
//hide
}

to reset the status! 重置状态! use 采用

new SharedPrefModel(this).clearInfo();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM