片段里面片段 [英] Fragment Inside Fragment

查看:131
本文介绍了片段里面片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要相关工作的片段,片段里帮忙,其实我 现在面临的pressing后退按钮的问题。应用程序主屏幕 有按钮和每个按钮查看pressing更换新 片段(和片段包含另一个片段内), 动态添加/替换片段做工精细,用pressing Button1的片段取代,同样的情况发生时pressing按钮,但如果 我再次preSS的按钮,有一个例外:

I need help regarding working on fragment inside fragment, actually I am facing a problem on pressing back button. Application Main screen has buttons and pressing on each button view replace with new fragment(and that fragment contain inside another fragment), dynamically adding/replacing fragment is working fine, by pressing button1 fragment replaced, same happens when pressing button, but if I 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 trying to add them again, anybody has idea how to work with fragment inside fragment and moving back and forth without any problem, thanks for the support.

MainActivity,其中片段动态添加和 更换。

MainActivity, where fragments are dynamical added and replaced.

   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().

这篇关于片段里面片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆