android如何在特定时隐藏导航drawer 'menuitems' fragment 选?

发布时间: 2016/10/10 22:15:38
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这里是登录 fragment :

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 fragment Login.I 尝试过很多,所以答案,但这不在我的情况下,所有的人都工作好与工作 Activity 但不是属 Fragment

谢谢你 time ...

解决方法 1:

fragment 没有实施 'NavigationView.OnNavigationItemSelectedListener' 当你这么做

navigationView.setNavigationItemSelectedListener(this);

这也是如此

public class Login extends Fragment implements
                                    NavigationView.OnNavigationItemSelectedListener

重写的方法和你是好去

更新︰ 到隐藏 / 显示 menuitemsnavigationView 时具体 fragment 选定

1) 初始化 navigationView 在你 activity

2) 里面导航listener。

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() 给你 menu 对象

findItem(id)给你 menuitem 以隐藏/显示

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

赞助商