在ActionBar选项卡下添加/替换片段中的片段

时间:2013-11-19 05:15:08

标签: android android-fragments android-actionbar fragmenttransaction fragmentmanager

我不确定我是否遗漏了任何基础知识。我有一个托管Action Bar标签的活动: 所以这是结构     MainActivity - >有两个ActionBar标签F1和F2 ||        Frag.java是容纳xml的容器片段,用于定义要替换的FrameLayout ||       F1是Frag1.java,其中Frag1.java显然是我们古老的片段 ||       F2是Frag2.java,其中Frag2.java是另一个片段 在我的MainActivity中,我创建了两个选项卡, * 注意: *两者都有相同的片段,这些片段是Frag.java,然后在Frag.java onCreateView中我尝试添加片段。这不会发生什么问题。

(理想情况下,我希望标签下的片段被按钮点击事件上的另一个片段替换)

这是我的主要活动

MainActivity.java

public class MainActivity extends SherlockFragmentActivity {
    @Override
    protected void onCreate(Bundle arg0) {

        super.onCreate(arg0);

        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


        ActionBar.Tab FragTab1 = actionBar.newTab().setText("F1");
        ActionBar.Tab FragTab2 = actionBar.newTab().setText("F2");

        Fragment fragment1 = new Frag();
        Fragment fragment2 = new Frag2();

        FragTab1.setTabListener(new MyTabListener(fragment1));
        FragTab2.setTabListener(new MyTabListener(fragment2));

        actionBar.addTab(FragTab1,0,true);
        actionBar.addTab(FragTab2,1,false);

    }


class MyTabListener implements ActionBar.TabListener
        {
            Fragment fragment;

            public MyTabListener(Fragment fragment)
            {
                this.fragment=fragment;
            }

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {}

            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {  }

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

来到我的简单片段我有三个设置Frag,Frag1,frag2

下面的

是frag.java

public class Frag extends SherlockFragment {

    public Frag() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.parent, container,false);

      FragmentManager fm = getFragmentManager();
      FragmentTransaction ft= fm.beginTransaction();


        Frag1 f1 = new Frag1();
        ft.add(R.id.contentFragment, f1);
        ft.commit();

        return  v;
    }


}
下面的

是我的parent.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" >

        <FrameLayout
            android:id="@+id/contentFragment"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >
        </FrameLayout>

    </LinearLayout>
下面的

是frag2.java

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.frag1, container, false);
        Button btn = (Button)v.findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Frag2 fragment = new Frag2();

                FragmentManager fm = getFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();
                transaction.replace(R.id.contentFragment, fragment);
                transaction.commit();

            }
        });
        return v;
    }

并在上面的片段中,您看到按钮单击应该替换片段,但我甚至无法获得此片段。

自从三天以来,我一直在玩弄这个。我真的很感激这里的任何帮助或建议,如果有人能指导我出错的地方,那将是非常棒的!

1 个答案:

答案 0 :(得分:1)

将您的MainActivity更改为

public class MainActivity extends SherlockFragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab FragTab1 = actionBar.newTab().setText("F1");
        ActionBar.Tab FragTab2 = actionBar.newTab().setText("F2");

        FragTab1.setTabListener(new TabListener<MyFragmentA>(this, "F1",
                Frag.class));
        FragTab1.setTabListener(new TabListener<MyFragmentB>(this, "F2",
                Frag2.class));

        actionBar.addTab(FragTab1, 0, true);
        actionBar.addTab(FragTab2, 1, false);

        if (savedInstanceState != null) {
            int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
            getSupportActionBar().setSelectedNavigationItem(savedIndex);
        }

    }

    public static class TabListener<T extends Fragment> implements
            ActionBar.TabListener {

        Fragment fragment;

        private final Activity myActivity;
        private final String myTag;
        private final Class<T> myClass;

        public TabListener(Activity activity, String tag, Class<T> cls) {
            myActivity = activity;
            myTag = tag;
            myClass = cls;
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {

            Fragment myFragment = myActivity.getFragmentManager()
                    .findFragmentByTag(myTag);

            // Check if the fragment is already initialized
            if (myFragment == null) {
                // If not, instantiate and add it to the activity
                myFragment = Fragment
                        .instantiate(myActivity, myClass.getName());
                ft.add(android.R.id.content, myFragment, myTag);
            } else {
                // If it exists, simply attach it in order to show it
                ft.attach(myFragment);
            }

        }

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

            Fragment myFragment = myActivity.getFragmentManager()
                    .findFragmentByTag(myTag);

            if (myFragment != null) {
                // Detach the fragment, because another one is being attached
                ft.detach(myFragment);

            }

        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

    }
}

单击选项卡时,片段会附加,当您选择另一个选项卡时,请分离当前片段并附加另一个片段。

不确定您在FragFrag2

中要执行的操作
 View v = inflater.inflate(R.layout.parent, container,false);

您在parent.xml中正在充气Frag。我相信这不是你想要的。相反,每个片段都有xml布局,并为其扩展布局。