0

昨天我下载了新的HoloEverywhere 库。目前,我在屏幕旋转后出现标签导航问题。

我的家庭活动:

public class MainActivity extends Activity implements TabListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setUpTabs();
    }

private void setUpTabs() {
    String[] titles = {
            "First", "Second",
    };

    ActionBar supportActionBar = getSupportActionBar();

    for (int i = 0; i < titles.length; i++) {
        ActionBar.Tab tab = supportActionBar.newTab();

        tab.setText(titles[i]);
        tab.setTag(MyFragment.TAG);

        tab.setTabListener(this);
        supportActionBar.addTab(tab, false);

    }

    supportActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    supportActionBar.setSelectedNavigationItem(0);

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
    final String fragmentTag = tab.getTag().toString();
    Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
    if (fragment == null) {
        fragment = new MyFragment();
        fragmentTransaction.add(android.R.id.content, fragment, fragmentTag);
    } else {
        fragmentTransaction.attach(fragment);
    }
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
     Fragment fragment = getSupportFragmentManager().findFragmentByTag((String)     tab.getTag());

    if (fragment != null) {
        fragmentTransaction.detach(fragment);
    }
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
} 

还有我的片段课。

public class MyFragment extends Fragment {
public static final String TAG = MyFragment.class.getCanonicalName();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = new View(getActivity());
    view.setBackgroundColor(Color.BLACK);
    return view;
}
}

当我旋转不显示的屏幕片段时。它在我手动选择选项卡(当前未选择)时显示。

4

1 回答 1

0

我只是解决问题。我在这里发布我的代码,看看是否可以帮助你:D

if (savedInstanceState == null){
    TabHomeFragment homeFragment = new TabHomeFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.container, homeFragment, "home_fragment").commit();
}else{
    TabHomeFragment homeFragment = (TabHomeFragment) getSupportFragmentManager().findFragmentByTag("home_fragment");
}

这些代码位于 OnCreate 方法中。当设备旋转和方向改变时,片段将再次重新创建。所以添加一个 if 类来检查这里是否已经有一个。

但我在 Android 中使用普通的 Fragment。希望它可以帮助你一点。

于 2013-05-21T08:06:35.197 回答