选项​​卡中切换标签

选项​​卡中切换标签

问题描述:

MainActivity.java

public class MainActivity extends FragmentActivity {
    // Fragment TabHost as mTabHost
    private FragmentTabHost mTabHost;

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

 mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
            Tab1Fragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
            Tab2Fragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
            Tab3Fragment.class, null);
    }
}

Tab1Fragment.java

public class Tab1Fragment extends Fragment  {

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View V = inflater.inflate(R.layout.tab1_view, container, false);

        return V;
    }
}

Tab2Fragment.java

public class Tab2Fragment extends Fragment  {

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View V = inflater.inflate(R.layout.tab2_view, container, false);

            return V;
        }
    }

Tab3Fragment.java

public class Tab3Fragment extends Fragment  {

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View V = inflater.inflate(R.layout.tab3_view, container, false);

            return V;
        }
    }

activity_main.xml

android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabWidget
        android:id="@android:id/tabs"

        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>
</android.support.v4.app.FragmentTabHost>


我的输出为 ::


My Output is ::

  • 点击设置页1 ----> 活动1 应显示。
  • 点击选定的设置页1 (再次)------> 活动2 应显示。
  • 点击选定的设置页1 (再次)对于第三次 ----> 活动 1 应显示再次
  • Click tab 1 ---- > Activity 1 should display.
  • Click on selected tab 1 (again) ------> Activity 2 should display.
  • Click on selected tab 1 (again) for third time ----> Activity 1 should display again.

类触发器自然

任何想法

香港乐施会,我会解释这一步一步 1.Just采取类像singlton类或应采取的任何类中的一个静态整型计数。 2.当过你点击选项卡,然后在选项卡clicklistner只是首先你检查什么是计数器的值,如果计数器== 1,那么第一个活动电话 如果计数器== 2那么第二个活动电话 计数器== 3,那么第三个活动通话

Ohk i will explain this step by step 1.Just take on class like singlton class or you should take one static int counter in any class. 2.when ever you click on tab then on tab clicklistner just first you check what is the value of counter if counter==1 then first activity call if counter==2 then 2nd activity call counter==3 then third activity call

和结束时只是增加你的计数器,如果计数器> 3则计数器= 1;

and at the end just increase your counter and if counter>3 then counter=1 ;

计数器是静态的,所以在应用程序的整个执行这将是常数,同样的:)

counter is static so in whole execution of app it will be constant and same :)