gpt4 book ai didi

android - 选择特定 fragment 时如何隐藏抽屉导航 `menuitems`?

转载 作者:搜寻专家 更新时间:2023-11-01 07:48:10 24 4
gpt4 key购买 nike

这是登录 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 登录。我尝试了很多SO答案,但在我的情况下都不起作用它们中的一部分在 Activity 中运行良好,但在 Fragment 中运行不佳。

感谢您的宝贵时间...

最佳答案

当你执行时,你的 fragment 没有实现'NavigationView.OnNavigationItemSelectedListener'

navigationView.setNavigationItemSelectedListener(this);

就这样吧

public class Login extends Fragment implements
NavigationView.OnNavigationItemSelectedListener

并覆盖方法,你就可以开始了

更新:在选择特定 fragment 时在 navigationView 中隐藏和显示 menuitems

1.) 在您的 Activity 中初始化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的状态修改为可见/不可见

关于android - 选择特定 fragment 时如何隐藏抽屉导航 `menuitems`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com