我的观点有问题。基本上我在这里使用片段是我的屏幕截图 照片A. 照片B.
不介意标签。当我点击照片A右上角的设置时,我会Settings Layout
然后,如果我点击帐户设置,则Settings Layout
替换为Account Detail
布局
之后如果我要退出,我会点击设置,但我的屏幕仍然显示“帐户详细信息”页面,即照片B,但必须是Settings Layout
(照片A)
我不知道如何实现这一目标。以下是我的代码。
设置按钮: 片段片段=> getFragmentManager()。findFragmentById( android.R.id.content); if(fragment!= null&& fragment.isInLayout()){ } else {
//fragment
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putString("user_name", user_name);
editor.putInt("user_id", user_id);
editor.commit();
Fragment mFragment = new SettingsFragment();
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(android.R.id.content, mFragment, "settings");
ft.commit();
}
在我的片段中:
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent(); //
System.out.println("asd"+parent);
if (parent != null) {
parent.removeView(view);
}
} else {
}
return view;
}
我的帐户设置按钮:
account.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
Fragment mFragment = new AccountSettings();
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.lists, mFragment);
// ft.addToBackStack(null);
ft.commit(); } });
请饶恕我,我是android的新手。任何建议和评论都会很有帮助。感谢
答案 0 :(得分:1)
例如:
这将使用此片段标记
弹出最后一个片段转换@Override
public void onBackPressed() {
FragmentManager fragmentManager = getSupportFragmentManager();
int stackSize = fragmentManager.getBackStackEntryCount();
if (stackSize == 0) {
finish();
}
else {
String fragmentTag = fragmentManager.getBackStackEntryAt(stackSize - 1).getName();
fragmentManager.popBackStack(fragmentTag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
这是一种做转换的方法
@Override public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CustomFragment fragment = new CustomFragment();
getActivity()
.getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.anim_back_right,R.animator.anim_back_left, R.animator.anim_left, R.animator.anim_right)
.add(R.id.custom_fragment, fragment)
.addToBackStack(MainActivity.THIS_FRAGMENT_TAG)
.commit();
}
这个想法就在这里
答案 1 :(得分:1)
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent(); //
System.out.println("asd"+parent);
if (parent != null) {
parent.removeView(view);
}
} else {
}
return view;
}
更改为
Fragment mFragment = new Settings();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println(view);
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
// System.out.println("asd"+parent);
if (parent != null) {
parent.removeView(view);
} else {
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.detach(mFragment);
ft.attach(mFragment);
ft.replace(R.id.lists, mFragment);
ft.commit();
//System.out.println("saadasd");
}
} else {
try {
view = inflater.inflate(R.layout.selectionfrag, container,
false);
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.lists, mFragment);
ft.commit();
} catch (InflateException e) {
}
}
return view;
}
我只需分离片段并附上它然后替换内容。
ft.detach(mFragment);
ft.attach(mFragment);
ft.replace(R.id.lists, mFragment);
但我不知道这是不是最好的做法。谁能说出来?