3

I want to use multiple fragments inside single tab. my tab activity extends SherlockFragmentActivity.

Now i add first fragment for first tab in onTabChanged() method. and i replace another fragment from within first fragment in first tab. But no when i go to second tab and then again re-open first tab, state of first tab not maintained and first fragment become visible rather than second fragment of first tab.

I need help for how to maintain stake of fragments within single tab and how to pop up exiting fragment front within single tab.

Any body have idea ?

4

2 回答 2

1

如果您使用两个选项卡,则必须为每个选项卡分别维护两个片段,即 Tab1 的 FragmentA 和 tab2 的 FragmentB 和 FragmentA 的 xmls , FragmentB 应该具有 FragmentTransaction 的 FrameLayout。

FragmentA 的 xml 即 frag_a.xml :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentA_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</FrameLayout>

FragmentB 的 xml 即 frag_b.xml :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentB_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</FrameLayout>

对于 Tab1,始终使用 R.id.fragmentA_container 执行事务。即从第一个选项卡中的第一个片段(片段A)中替换另一个片段。像下面的代码

在片段A中:

Fragment fragment = new YourFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
getSupportFragmentManager().popBackStack();
fragment.setArguments(null);
ft.replace(R.id.fragmentA_container, fragment);
ft.commit();

对于 Tab2,始终使用 R.id.fragmentB_container 执行事务。即从第二个选项卡中的 secondfragment(FragmentB) 中替换另一个片段。像下面的代码

在片段B中:

Fragment fragment = new YourFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
getSupportFragmentManager().popBackStack();
fragment.setArguments(null);
ft.replace(R.id.fragmentB_container, fragment);
ft.commit();
于 2013-07-26T06:01:42.357 回答
0

以下是管理活动中多个片段的示例代码:

public class MainActivity extends FragmentActivity {

private FragmentManager fragmentManager;
Fragment currentFragment;
Fragment1 initialFragment;
Fragment2 nextFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    fragmentManager = getSupportFragmentManager();
    initialFragment = (Fragment1) fragmentManager.findFragmentById(R.id.initial_fragment);
    nextFragment = (Fragment2) fragmentManager.findFragmentById(R.id.next_fragment);
    nextFragment();
}

public void onNext(View v) {
    // User pressed next button on initial fragment
    showNextFragment();
}

private void showFragment(Fragment fragmentToShow) {
    Fragment[] fragments = { initialFragment, nextFragment /*... */ };
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    for(Fragment fragment : fragments) {
        if(fragment != fragmentToShow) {
            fragmentTransaction.hide(fragment);
        }
    }
    fragmentTransaction.show(fragmentToShow);
    fragmentTransaction.commit();
}

private void showInitialFragment() {
    currentFragment = initialFragment;
    showFragment(currentFragment);
}

private void showNextFragment() {
    currentFragment = nextFragment;
    showFragment(currentFragment);
}

}
于 2013-07-25T09:52:11.083 回答