1

我正在尝试在 android 中实现 fragment-Tabhost

MainActivity.java

public class MainActivity extends Activity {
    // Declare Tab Variable
    ActionBar.Tab Tab1, Tab2;

    Fragment fragmentTab2 = new FragmentTab2();
    //Fragment fragmentTab3 = new FragmentTab3();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();

        // Hide Actionbar Icon
        actionBar.setDisplayShowHomeEnabled(false);

        // Hide Actionbar Title
        actionBar.setDisplayShowTitleEnabled(false);

        // Create Actionbar Tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set Tab Icon and Titles
        Tab1 = actionBar.newTab().setText("Tab1");
        //Tab2 = actionBar.newTab().setText("Tab2");

        // Set Tab Listeners
        Tab1.setTabListener(new TabListener(fragmentTab2));
        //Tab2.setTabListener(new TabListener(fragmentTab3));

        // Add tabs to actionbar
        actionBar.addTab(Tab1);
        //actionBar.addTab(Tab2);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

FragmentTab2.java

public class FragmentTab2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragmenttab2, container, false);
        return rootView;
    }

}

FragmentTab3.java

public class FragmentTab3 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragmenttab3, container, false);
        return rootView;
    }

}

目前只显示一个带有一项活动的选项卡

我想做什么::

  • 点击tab1(第一次) - - - - >FragmentTab2.java必须显示
  • 点击tab1(第二次) - - - - >FragmentTab3.java必须显示
  • 点击tab1(第三次)- - - - >FragmentTab2.java必须再次显示

这个循环必须不断重复

我需要进行哪些代码更改?有任何想法吗 ?

4

2 回答 2

0

您需要实现TabListener并设置为Tab1.setTabListener().

onTabSelected(),重复替换fragment1 或fragment2。

于 2013-10-24T08:39:27.737 回答
0

我让它工作了,你仍然需要做一些调整,但它会让你开始。

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity implements Notify, TabListener {

    ActionBar.Tab tab1;
    ActionBar actionBar;
    int count = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getFragmentManager().beginTransaction().replace(R.id.container, new FragmentTab2()).commit();
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        tab1 = actionBar.newTab().setText("Tab1");
        tab1.setTabListener(this);
        actionBar.addTab(tab1);
    }
    //Callback
    @Override
    public void notifyTabListener() {
        count++;
        Tab tab = actionBar.newTab().setText("Tab" + count);
        tab.setTabListener(this);
        actionBar.addTab(tab);
    }
    //Tablistener
    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.container, new FragmentTab2());
    }
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.add(new FragmentTab2(), "Tab" + count);
    }
    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }
}

通知回调:

public interface Notify {
    public void notifyTabListener();
}

片段选项卡2:

public class FragmentTab2 extends Fragment implements OnClickListener {

    Notify mCallback;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.f2, container, false);
        LinearLayout  layout = (LinearLayout) rootView.findViewById(R.id.layout2);
        layout.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mCallback = (MainActivity) activity;
    }

    @Override
    public void onClick(View v) {
        mCallback.notifyTabListener();
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</RelativeLayout>

f2.xml

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

<TextView android:text="Fragment2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>
于 2013-10-24T09:38:16.000 回答