使用操作栏即时“复制”片段

时间:2012-08-28 08:03:29

标签: android android-fragments android-actionbar

我在这里读了几篇关于分离和附加的帖子。但是因为我正在运行API 15,所以应该没问题。

我从头开始得到一个双重列表,就像你在这张图片上看到的那样: image of activity

我有以下代码: (MainActivity)

   package dk.metnik.fragmentandactionbar;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import dk.metnik.fragmentandactionbar.fragments.DCIMFragment;
import dk.metnik.fragmentandactionbar.fragments.ESSFragment;
import dk.metnik.fragmentandactionbar.fragments.TeamshareFragment;

public class FragAndBarActivity extends Activity {

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

    private void setupTabs() {
        // setup action bar for tabs
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);

        Tab tab = actionBar
                .newTab()
                .setText("DCIM")
                .setTabListener(
                        new MyTabListener<DCIMFragment>(this, "DCIM",
                                DCIMFragment.class));
        actionBar.addTab(tab);

        tab = actionBar
                .newTab()
                .setText("ESS")
                .setTabListener(
                        new MyTabListener<ESSFragment>(this, "ESS",
                                ESSFragment.class));
        actionBar.addTab(tab);

        tab = actionBar
                .newTab()
                .setText("TeamShare")
                .setTabListener(
                        new MyTabListener<TeamshareFragment>(this, "TeamShare",
                                TeamshareFragment.class));
        actionBar.addTab(tab);
    }

    public static class MyTabListener<T extends Fragment> implements
            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 MyTabListener(Activity activity, String tag, Class<T> clz) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;

            mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
            if (mFragment != null) { // && !mFragment.isDetached()) {
                FragmentTransaction ft = mActivity.getFragmentManager()
                        .beginTransaction();
                // ft.detach(mFragment);
                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 == null) {
                // If not, instantiate and add it to the activity
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.add(R.id.listFragment, mFragment, mTag);
            } else {
                ft.attach(mFragment);
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            if (mFragment != null) {
                ft.detach(mFragment);
            }
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    }
}

我的main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/listFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"/>
</LinearLayout>  

1 个答案:

答案 0 :(得分:1)

您通过layout.xml使用Fragment实例化视图:

<fragment
    android:id="@+id/listFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?android:attr/actionBarSize"
    class="dk.metnik.fragmentandactionbar.fragments.TeamshareFragment" />

然后你创建了一个Tab系统,其中将选择第一个标签,在顶上添加另一个片段(我猜是透明的背景)。

删除class=""并将<fragment>更改为<FrameLayout>。这将停止最初的片段加载,同时为您提供定义的视图以容纳片段。

此外,您要将新片段添加到Android基础视图android.R.id.content。这是合理的,具体取决于您的功能,但如果您希望Fragment占用元素的空间,您应该使用其id。变化:

ft.add(android.R.id.content, mFragment, mTag); 

ft.add(R.id.listFragment, mFragment, mTag);