如何在ActionBar下的同一个标签内更改片段?

时间:2013-11-08 09:41:42

标签: android tabs android-actionbar fragment

我的问题是片段和ActionBar(android.support.v7.app.ActionBar)

我遵循了这个example,特别是“添加导航标签”

部分

该示例工作正常,但是当我想用另一个片段更改选项卡(因此片段)的内容时,我遇到了问题。

在这个例子中,我在第一个片段“Tab_Fragment_1”(第一个标签下)添加了一个按钮。 当按下按钮时,我想改变内容(因此加载另一个片段),显然在同一个标​​签下。

在MainActivity中,我添加了方法“clickbutton”,它在tab_fragment_1'layout

中定义

我不知道要遵循的模式。我该怎么办?

以下是我的代码:

MainActivity

    public class MainActivity extends ActionBarActivity {

    private String TAG="MainActivity";

    Tab tab =null;

    Tab_Fragment_2A tab_Fragment_2A=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "msg-1");


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

        tab = actionBar.newTab()
                           .setText(R.string.tab_1)
                           .setTabListener(new TabListener<Tab_Fragment_1>(
                                   this, "tab_1", Tab_Fragment_1.class));
        actionBar.addTab(tab);

        tab = actionBar.newTab()
                .setText(R.string.tab_2)
                .setTabListener(new TabListener<Tab_Fragment_2>(
                        this, "tab_2", Tab_Fragment_2.class));
        actionBar.addTab(tab);

        tab = actionBar.newTab()
                .setText(R.string.tab_3)
                .setTabListener(new TabListener<Tab_Fragment_3>(
                        this, "tab_3", Tab_Fragment_3.class));
        actionBar.addTab(tab);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        Log.d(TAG, "msg-2");
        getMenuInflater().inflate(R.menu.main, menu);
        Log.d(TAG, "msg-3");
        return true;
    }

    public static 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.add(android.R.id.content, mFragment, mTag);
            } else {
                // If it exists, simply attach it in order to show it
                ft.attach(mFragment);
            }
        }

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

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

    public void clickbutton(View v) {
        Log.d(TAG, "button clicked");

        //to do?


    }


}

activity_main.xml中

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
</RelativeLayout>

Tab_Fragment_1课程:

public class Tab_Fragment_1 extends Fragment{


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.tab_fragment_1,container, false);
        return view;
    }



}

tab_fragment_1:

    <?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" 
    android:background="@color/Azure">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="clickbutton" />

</LinearLayout>

0 个答案:

没有答案