Android层面的Fragments与ActionBar的结合< 13、有没有可行的办法?

发布于 2025-01-06 12:09:40 字数 3416 浏览 3 评论 0 原文

我想根据我的要求使用尽可能低的 Android 版本编写我的项目,即 11。

但我需要 Fragment.attach 和 ActionBars。

由于级别 11 不包括 Fragment.attach 我导入了 v4 的支持包。

但现在的问题是,ActionTab 的 TabListerner 不使用 v4 Fragment,而是使用 level 11 Fragment。选角不行。

我真的需要切换到13级吗?还是有一个可行的解决方案可以在11级中实现这一切。

代码如下:

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) {

    }

    }
}

I would like to write my project in the lowest possible Android version for my reqs, which is 11.

But I need Fragment.attach and ActionBars.

Since level 11 does not include Fragment.attach I import the support package for v4.

But now the problem is that the TabListerner for the ActionTab does not use the v4 Fragment but rather the level 11 Fragment. Casting won't work.

Do I really need to switch to level 13 or is there a feasible solution to implement all this in level 11.

Here is the 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) {

    }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靖瑶 2025-01-13 12:09:40

我在 Api Ver 8.0 上使用 ActionBarCompat 和 Fragments ,与您的代码的唯一区别是我使用 ft.replace 而不是 ft.add 以及 xml 上的 ViewPager 来显示片段,到目前为止它运行正常..

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.
    }
}

im using ActionBarCompat and Fragments on Api Ver 8.0 , the only diference with your code its that im using ft.replace instead of ft.add and a ViewPager on the xml to show the fragments, it runs ok so far ..

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.
    }
}
年华零落成诗 2025-01-13 12:09:40

不确定您是否仍然需要这个,但我们使用 ActionBarSherlock ,听起来它也可以帮助您:

http://actionbarsherlock.com /

Not sure if you still need this, but we use ActionBarSherlock and it sounds like it could help you as well:

http://actionbarsherlock.com/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文