在我的MainActivity中,这是我的选项菜单代码 -
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_profile) {
return true;
}
if (id == R.id.action_password) {
return true;
}
if (id == R.id.action_update) {
return true;
}
if (id == R.id.action_signin) {
return true;
}
if (id == R.id.action_help) {
return true;
}
if (id == R.id.action_use_terms) {
return true;
}
if (id == R.id.action_policy) {
return true;
}
return super.onOptionsItemSelected(item);
}
创建了一个名为fragment_changepassword.xml和ChangePassword.java的片段。 现在我想从 -
调用该片段if (id == R.id.action_password) {
return true;
}
这一部分,但搜索谷歌但没有得到任何帮助,终于带着希望来到这里。
从Tab中调用其他片段更容易 -
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
} else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
View rootView = inflater.inflate(R.layout.fragment_videos, container, false);
return rootView;
} else if (getArguments().getInt(ARG_SECTION_NUMBER) == 3){
View rootView = inflater.inflate(R.layout.fragment_messages, container, false);
return rootView;
}
else if (getArguments().getInt(ARG_SECTION_NUMBER) == 4){
View rootView = inflater.inflate(R.layout.fragment_audios, container, false);
return rootView;
}
else {
View rootView = inflater.inflate(R.layout.fragment_images, container, false);
return rootView;
}
}
答案 0 :(得分:0)
要从活动中设置容器(在xml中定义)中的片段,必须使用SupportFragmentManager。 试试这段代码
if (id == R.id.action_password) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, ChangePasswordFragment.newInstance())
.commit();
return true;
}
在片段calss中提供newInstance()
方法。像这样的东西 -
public static ChangePasswordFragment newInstance() {
return new ChangePasswordFragment();
}