当选择特定片段时,如何隐藏导航抽屉`menuitems`?

时间:2016-09-17 06:15:20

标签: android android-fragments android-navigation-drawer

这是Login片段:

public class Login extends Fragment {

    TextInputEditText u_name,u_pass;
    String name_user,password_user;
    Button login_btn;
    TextView user_register;
    String PREF="myData";
    SharedPreferences preferences;
    SharedPreferences.Editor editor;
    NavigationView navigationView;


    public Login() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view= inflater.inflate(R.layout.fragment_login, container, false);
        u_name=(TextInputEditText) view.findViewById(R.id.user_name);
        u_pass=(TextInputEditText) view.findViewById(R.id.user_password);
        login_btn=(Button) view.findViewById(R.id.submit_btn);
        user_register=(TextView) view.findViewById(R.id.register_user);

        //setting up navigationview text and image

        navigationView=(NavigationView) view.findViewById(R.id.nav_view);

        navigationView.setNavigationItemSelectedListener(this);

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name_user=u_name.getText().toString().trim();
                password_user=u_pass.getText().toString();

                if (name_user.isEmpty())
                {
                    u_name.setError("Enter email address");
                }
                else if (password_user.isEmpty())
                {
                    u_pass.setError("Enter password");
                }
                else
                {
                    loginUser();
                }

            }
        });

        user_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                FragmentManager fragmentManager=getFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.containerView,new RegisterUser()).addToBackStack(null).commit();

            }
        });
        return view;
    }

    private void loginUser()
    {
        final ProgressDialog pd=ProgressDialog.show(getActivity(),"Login","Please wait",false);
        StringRequest loginRequest=new StringRequest(Request.Method.POST, "http://mywebdata.com/webservice/logincheck.php", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                pd.dismiss();
                //Toast.makeText(getActivity(), ""+response, Toast.LENGTH_LONG).show();
                if (response.contains("Invalid Email Address and Password"))
                {
                    Toast.makeText(getActivity(), "Invalid Email Address and Password", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    getLoginUser(response);
                }


                //Log.e("login",""+response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                pd.dismiss();
                Log.e("Error",""+error.toString());
                Toast.makeText(getActivity(), "Network error please try again", Toast.LENGTH_SHORT).show();

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<String, String>();
                map.put("usrname",name_user);
                map.put("passwrd",password_user);
                return map;
            }
        };
        RequestQueue queue= Volley.newRequestQueue(getActivity());
        queue.add(loginRequest);
    }

    private void getLoginUser(String user)
    {
        ParseJson parseJson=new ParseJson(user);
        parseJson.parseUserLogin();

        if (ParseJson.login_mail[0].equals(name_user))
        {
            preferences=getActivity().getSharedPreferences(PREF, Context.MODE_PRIVATE);
            editor=preferences.edit();
            editor.putString("LOGIN_USER",ParseJson.login_id[0]);
            editor.putString("LOGIN NAME",ParseJson.login_name[0]);
            editor.commit();
        }
        else
        {
            Toast.makeText(getActivity(), "Invalid user name,Try again", Toast.LENGTH_SHORT).show();
        }
    }

}

问题是,什么时候调用navigationView.setNavigationItemSelectedListener(this);它给了我错误,我无法调用这个witihn片段Login.I尝试了很多SO答案,但在我的情况下不起作用所有这些都与Activity一起工作正常但没有在片段中。

感谢您的时间......

2 个答案:

答案 0 :(得分:2)

fragment/activity中实施以下界面:

NavigationView.OnNavigationItemSelectedListener

oncreate/oncreateView

中添加以下代码
navigationView.setNavigationItemSelectedListener(this); 

答案 1 :(得分:2)

当您执行

时,您的片段没有实现'NavigationView.OnNavigationItemSelectedListener'
navigationView.setNavigationItemSelectedListener(this);

这样做

public class Login extends Fragment implements
                                    NavigationView.OnNavigationItemSelectedListener

并覆盖方法,你很高兴

更新:在选择特定片段时隐藏menuitems {/ 1}}

1。)初始化活动中的navigationView

2。)内部导航监听器

navigationView

其中navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { if(menuItem.getItemId()==R.id.login_fragment ) { // to hide items navigationView.getMenu().findItem(R.id.item_to_hide).setVisible(false); // to show items navigationView.getMenu().findItem(R.id.item_to_show).setVisible(true); } return true; } }); 为您提供菜单对象

getMenu()为您提供findItem(id)隐藏/显示

menuitem会将setVisibile()的状态修改为可见/不可见