La combinaison de fragments et ActionBar dans le niveau Android <13. Est-il possible possible?

StackOverflow https://stackoverflow.com/questions/9342777

Question

Je voudrais écrire mon projet dans le plus bas possible version Android pour mes reqs, ce qui est 11.

Mais je dois Fragment.attach et ActionBars.

Puisque le niveau 11 ne comprend pas Fragment.attach importer le paquet de soutien à v4.

Mais le problème est que le TabListerner pour la ActionTab n'utilise pas le v4 Fragment mais le niveau 11 Fragment. Coulée ne fonctionnera pas.

Ai-je vraiment besoin de passer à un niveau 13 ou est-il une solution possible de mettre en œuvre tout cela dans le niveau 11.

Voici le code:

import android.app.ActionBar;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;

import android.os.Bundle;
import android.support.v4.app.Fragment;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }

    public static class TabListener<T extends android.support.v4.app.Fragment> /* to make sure it take the Fragment from the support package! */
    implements ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /**
     * Constructor used each time a new tab is created.
     * 
     * @param activity
     *            The host Activity, used to instantiate the fragment
     * @param tag
     *            The identifier tag for the fragment
     * @param clz
     *            The fragment's Class, used to instantiate the fragment
     */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

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

    public void onTabSelected(Tab tab,
        android.support.v4.app.FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
        // If not, instantiate and add it to the activity
        mFragment = Fragment.instantiate(mActivity, mClass.getName());
        ft.add(android.R.id.content, mFragment, mTag);
        } else {
        // If it exists, simply attach it in order to show it
        ft.attach(mFragment);
        }
    }

    /* these are NOT the implementation of the TabListener above, since the use the
     * 
     * the  FragmentTransactionof the support package and not of level 11
     * 
     */

    public void onTabUnselected(Tab tab,
        android.support.v4.app.FragmentTransaction ft) {
        if (mFragment != null) {
        // Detach the fragment, because another one is being attached
        ft.detach(mFragment);
        }
    }

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

    /* these are added since they belong to the above definition of TabListener 
     * 
     * unfortunately the use the FragmentTransaction of level 11, not the one of the support package!
     * 
     */
    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
    }

    @Override
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {

    }

    }
}
Était-ce utile?

La solution

Je ne sais pas si vous avez encore besoin de cela, mais nous utilisons ActionBarSherlock et il semble que cela pourrait vous aider ainsi:

http://actionbarsherlock.com/

Autres conseils

im en utilisant ActionBarCompat et Fragments sur Api Ver 8.0, la seule diférence avec votre code en utilisant son qui im ??ft.replace au lieu de ft.add et un ViewPager sur le xml pour montrer les fragments, il passe bien jusqu'à présent ..

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;

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

    /** Constructor used each time a new tab is created.
      * @param activity  The host Activity, used to instantiate the fragment
      * @param tag  The identifier tag for the fragment
      * @param clz  The fragment's Class, used to instantiate the fragment
      */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* 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 == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.replace(R.id.pager, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.replace(R.id.pager, mFragment, mTag);
        }
    }

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

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top