问题描述
我需要相关工作的片段,片段里帮忙,其实我现在面临的pressing后退按钮的问题。应用程序主屏幕有按钮和每个按钮查看pressing更换新片段(和片段包含另一个片段内),动态添加/替换片段做工精细,用pressingButton1的片段取代,同样的情况发生时pressing按钮,但如果我再次preSS的按钮,有一个例外:
I need help regarding working on fragment inside fragment, actually Iam facing a problem on pressing back button. Application Main screenhas buttons and pressing on each button view replace with newfragment(and that fragment contain inside another fragment),dynamically adding/replacing fragment is working fine, by pressingbutton1 fragment replaced, same happens when pressing button, but ifI press the button again, got an exception:
"Duplicate id 0x7f05000a, tag null, or parent id 0x7f050009 with
another fragment for com........ fragmentname"
指片段或内部片段已经增加,我想再次添加它们,任何人有知道如何与内部片段工作片段和来回移动没有任何问题,感谢支持。
means fragment or inner fragments are already added and I am tryingto add them again, anybody has idea how to work with fragment insidefragment and moving back and forth without any problem, thanks for thesupport.
MainActivity,其中片段动态添加和更换。
MainActivity, where fragments are dynamical added andreplaced.
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);
}
}
下面是布局:main.xml中
Here is 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>
希望我想清楚我的问题。
Hope I tried to clear my problem.
推荐答案
AFAIK,片段不能持有其他片段。
AFAIK, fragments cannot hold other fragments.
更新
随着Android支持包的最新版本 - 在API级别17或更高的或天然片段 - 你可以嵌套的片段,由手段getChildFragmentManager()
。请注意,这意味着你需要使用碎片对API级别11-16 Android的支持包的版本,因为即使有碎片在这些设备上的本机版本,该版本不具有 getChildFragmentManager( )
。
With current versions of the Android Support package -- or native fragments on API Level 17 and higher -- you can nest fragments, by means of getChildFragmentManager()
. Note that this means that you need to use the Android Support package version of fragments on API Levels 11-16, because even though there is a native version of fragments on those devices, that version does not have getChildFragmentManager()
.
这篇关于片段里面片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!