我想要对fragment中的fragment进行操作,在按后退按钮的时候出现了问题。应用程序主屏幕上有很多按钮,按下每个按钮都会出现新的fragment(它也被包含在另一个fragment内)。按下按钮1(已替换fragment),可以进行动态的添加或者是替换片段。如果按下按钮后,再去按它,就会得到:
"Duplicate id 0x7f05000a, tag null, or parent id 0x7f050009 with
another fragment for com........ fragmentname"
这意味着已经添加过fragment,有谁可以帮我完成fragment的相关设定,并顺利的完成来回切换?
如下是,MainActiviy的代码,它可以控制动态添加/替换fragment:
public class FragmentInsideFragmentTestActivity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 =(Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onButtonClick(view);
}
});
button2 =(Button) this.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onButtonClick(view);
}
});
button3 =(Button) this.findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onButtonClick(view);
}
});
button4 =(Button) this.findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onButtonClick(view);
}
});
}
public void onButtonClick(View v) {
Fragment fg;
switch (v.getId()) {
case R.id.button1:
fg=FirstFragment.newInstance();
replaceFragment(fg);
break;
case R.id.button2:
fg=SecondFragment.newInstance();
replaceFragment(fg);
break;
case R.id.button3:
fg=FirstFragment.newInstance();
replaceFragment(fg);
break;
case R.id.button4:
fg=SecondFragment.newInstance();
replaceFragment(fg);
break;
}
}
private void replaceFragment(Fragment newFragment) {
FragmentTransaction trasection =
getFragmentManager().beginTransaction();
if(!newFragment.isAdded()){
try{
//FragmentTransaction trasection =
getFragmentManager().beginTransaction();
trasection.replace(R.id.linearLayout2, newFragment);
trasection.addToBackStack(null);
trasection.commit();
}catch (Exception e) {
// TODO: handle exception
//AppConstants.printLog(e.getMessage());
}
}else
trasection.show(newFragment);
}
}
如下是Layout : main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="wrap_content"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:text="Button1"></Button>
<Button android:text="Button2"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="Button3"
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="Button4"
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
以上就是我遇到的问题,希望大家可以为我解答。
答:CommonsWare
(最佳答案)
你可以用getChildFragmentManager()进行fragment嵌套操作,但这也意味着你的Android支持包版本的API级别要在11-16之间。主要是因为,即使在配置上存在fragment的自带版本,但是也未必可以支getChildFragmentManager()。
答:Napolean
fragment可以嵌套在其它的fragment中,但每次调用onDestroyView()时,你都需要从parent Fragment中删掉它,然后将它添加到OnCreateView()中的parent Fragment。具体方法如下:
答:Vetal.lebed
我的解决办法是,使用支持库(Support library)和ViewPager。你可以禁用一些不必要的程序,具体方法如下: