1

我不确定我是否在这里遗漏了任何基础知识。我有一个托管操作栏选项卡的活动:所以这里是 MainActivity 的结构 -> 有两个操作栏选项卡 F1 和 F2 || Frag.java 是承载定义要替换的 FrameLayout 的 xml 的容器片段|| 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;
    }

在上面的片段中,您看到按钮单击应该替换片段,但我什至无法将这个片段放置到位。

自从三天以来,我一直在玩弄这个。我真的很感激这里的任何帮助或建议,或者如果有人能指导我哪里出错了,那就太棒了!

4

1 回答 1

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 布局,并为相同的片段扩展布局。

于 2013-11-19T05:43:15.127 回答