1

在管理由 Fragments 表示的选项卡界面时,我在管理它们时遇到了问题。首次启动应用程序时,第一个/默认片段显示在显示屏上。但是,当我单击选项卡切换 Fragments 时,屏幕会在第二个 Fragment 充气显示之前闪烁。在显示第二个片段后,我可以在两者之间无缝切换。是否可以在应用程序启动期间对两个 Fragment 进行预充气?这是代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    favoritesFragment = new FavoritesTab();
    searchFragment = new NearbyTab();


// Database initialization
    db = new DatabaseHandler(this);
    prev_stops = db.getAllBusStop();

    ab = getActionBar();
    ab.setBackgroundDrawable(new ColorDrawable(Color.DKGRAY));
    ab.setStackedBackgroundDrawable(new ColorDrawable(Color.GRAY));
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tab1 = ab.newTab().setText("Favorites")
            .setTabListener(new TabListener<FavoritesTab>(
                    this, "tabone", FavoritesTab.class, favoritesFragment));

    Tab tab2 = ab.newTab().setText("Nearby")
            .setTabListener(new TabListener<NearbyTab>(
                    this, "tabtwo", NearbyTab.class, searchFragment));

    ab.addTab(tab1, 0, true);
    ab.addTab(tab2, 1, false);

}

public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Bundle mArgs;
    private final Class<T> mClass;


    public TabListener(Activity activity, String tag, Class<T> clz, Fragment fragment) {
        this(activity, tag, clz, null, fragment);
    }

    public TabListener(Activity activity, String tag, Class<T> clz, Bundle args, Fragment fragment) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;
        mFragment = fragment;

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.

        //mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null /*&& !mFragment.isDetached()*/) {
            System.out.println("In TabListener constructor now");
            FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
            ft.remove(mFragment);
            ft.commit();
        }
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if(mFragment.isHidden()) {
            System.out.println("Initial loop");
            ft.show(mFragment);
        }
        else { 
            System.out.println("Non-initial loop");
            ft.add(R.id.fragment_container, mFragment);
        }

    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.hide(mFragment);
            //ft.remove(mFragment);

            curr_tab = tab.getPosition();
            if(curr_tab == 1) {
                item3.setIcon(R.drawable.refresh);
            }
            else {
                item3.setIcon(R.drawable.location_found);
            }
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}

附带说明一下,在切换选项卡时是否可以为此设置提供类似于 ViewPager 的滑动/滑动动画?

4

0 回答 0