2016-09-17 57 views
2

下面是登錄片段:如何在選擇特定片段時隱藏導航抽屜`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中。

感謝您的時間...

+2

您必須實現NavigationView.OnNavigationItemSelectedListener在您登錄片段 – darwin

回答

2

您的片段還沒有實現'NavigationView.OnNavigationItemSelectedListener'當你做

navigationView.setNavigationItemSelectedListener(this); 

這樣做

public class Login extends Fragment implements 
            NavigationView.OnNavigationItemSelectedListener 

和覆蓋的方法,你是好去

更新:當特定片段選擇

1.要隱藏和顯示navigationViewmenuitems)初始化navigationView您的活動

2.)內置導航監聽器

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()將修改的menuitem狀態,以顯示/隱藏

+0

感謝快速答覆...讓我先試 – Manish

+0

它給我的另一個錯誤**嘗試調用虛擬方法「無效android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView $ OnNavigationItemSelectedListener)」上一個空對象引用** – Manish

+1

意味着你'navigationView'沒有正確初始化。檢查你的'fragment_login'佈局應了'navigationView'這個'nav_view' ID –

1

fragment/activity實現以下接口:

NavigationView.OnNavigationItemSelectedListener 

添加以下代碼裏面oncreate/oncreateView

navigationView.setNavigationItemSelectedListener(this);