下面是登錄片段:如何在選擇特定片段時隱藏導航抽屜`menuitems`?
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中工作正常,但不在Fragment中。
感謝您的時間...
您必須實現NavigationView.OnNavigationItemSelectedListener在您登錄片段 – darwin