2012-05-23 89 views
0

我有一个应用程序,它有一个用5个选项卡设置的TabHost。这些标签是片段活动。在每个这些片段活动中,都有几个片段被设置。片段活动转换

我的主类设置了tabHost和片段活动,像这样

 public class Main extends TabActivity 
     { 

      public void onCreate(Bundle savedInstanceState) 
      { 
       super.onCreate(savedInstanceState); 
       requestWindowFeature(Window.FEATURE_NO_TITLE); 
       setContentView(R.layout.main); 

      Resources res = getResources(); 
       tabHost = getTabHost(); 
       TabHost.TabSpec spec; 
       Intent intent; 

     intent = new Intent().setClass(this, 
               Tab1.class); 
        spec = tabHost 
        .newTabSpec("home") 
              .setIndicator("Home", 
                res.getDrawable(R.drawable.tabhome)) 
              .setContent(intent); 
            tabHost.addTab(spec); 
     //and do this for the other 4 tabs 

    tabHost.setCurrentTab(0); 
} 

     } 

然后我fragmentActivities设置了这样

public class Tab1 extends FragmentActivity 
{ 

    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.home); 

FragmentTransaction fragmentTransaction = getSupportFragmentManager() 
       .beginTransaction(); 
     // Fragments 
     Fragment fragOne = new FragmentOne(); 
     Fragment fragTwo = new FragmentTwo(); 
     Fragment fragThree = new FragmentThree(); 

     fragmentTransaction.replace(R.id.fragmentOne, fragOne); 
     fragmentTransaction.replace(R.id.fragmentTwo, fragTwo); 
     fragmentTransaction.replace(R.id.fragmentThree, fragThree); 

     fragmentTransaction.commit(); 

     } 
} 

TAB1片段活动有3个片段,你可以在上面看到。每个片段,FragmentOne,FragmentTwo和FragmentThree都包含片段中的下一个按钮。

我想知道的是如何设置它,以便它们在类内的每个片段之间转换。例如。 (在Frag2中单击下一个按钮)Frag3从右侧过渡(在Frag3中,单击下一个按钮)Frag1从右侧过渡。Frag1从右侧过渡(在Frag2中单击下一个按钮)。

任何援助将不胜感激!

编辑:还会粘贴如何在home.xml布局文件中设置片段,以免您想知道如何设置它。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/homeMain" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <RelativeLayout 
        android:id="@+id/FragmentOneRL" 
        android:layout_width="wrap_content" 
        android:layout_height="100dp" 
        android:orientation="vertical" > 

        <FrameLayout 
         android:id="@+id/fragmentOne" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" > 
        </FrameLayout> 
       </RelativeLayout> 

       <RelativeLayout 
        android:id="@+id/FragmentTwoRL" 
        android:layout_width="wrap_content" 
        android:layout_height="100dp" 
        android:layout_below="@+id/FragmentOneRL" 
        android:orientation="vertical" > 

        <FrameLayout 
         android:id="@+id/fragmentTwo" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" > 
        </FrameLayout> 
       </RelativeLayout> 

       <RelativeLayout 
        android:id="@+id/FragmentThreeRL" 
        android:layout_width="wrap_content" 
        android:layout_height="100dp" 
        android:layout_below="@+id/FragmentTwoRL" 
        android:orientation="vertical" > 

        <FrameLayout 
         android:id="@+id/fragmentThree" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" > 
        </FrameLayout>  

       </RelativeLayout> 

     </RelativeLayout> 

回答

0

我终于想通了,我的XML文件更改为此

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

      <FrameLayout 
        android:id="@+id/TheFragment" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" > 
       </FrameLayout> 

</RelativeLayout> 

然后在FragmentActivity我在每个片段中创造了这个方法

protected void changeFragment(Fragment removeFrag, Fragment f) 
    { 
     FragmentTransaction ft = fm.beginTransaction(); 
     ft.setCustomAnimations(R.anim.view_transition_in_left, 
       R.anim.view_transition_out_left); 
     ft.replace(R.id.TheFragment, f); 
     ft.remove(removeFrag); 
     ft.addToBackStack(null); 
     ft.commit(); 
    } 

然后,当用户点击下一步按钮会这样做

Fragment f = ((Tab1) getActivity()).fragTwo; 
      Fragment rf = ((Tab1) getActivity()).fragOne; 


      ((Tab1) getActivity()).changeFragment(rf, f);