2014-10-29 32 views
0

我正在使用片段,而我的問題是如何在片段中打開類Test1和Test2。其實我使用意圖的重定向活動,但我想使用片段不活動。 Possibile如何?如何傳遞內部TabHost中的片段android

I need to two tab. 1)App 2)Application. and Successfully Create both tab. but using with fragment Like TabHost. 

請給我建議。 我的代碼下面,

我的代碼,

  import android.app.Fragment; 
      import android.os.Bundle; 
      import android.view.LayoutInflater; 
      import android.view.View; 
      import android.view.ViewGroup; 
      import android.widget.AnalogClock; 
      import android.widget.TabHost; 

      import com.example.app.R; 

      public class CallFragment extends Fragment implements OnTabChangeListener{ 
       private TabHost tabHost = null; 
       View rootView = null; 
       TabHost.TabSpec spec, spec1; 

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

        tabHost = (TabHost) rootView.findViewById(R.id.tab_host); 
        tabHost.setup(); 
        spec = tabHost.newTabSpec("tagApp"); 
        spec1 = tabHost.newTabSpec("tagApplication"); 
        spec.setIndicator("App"); 
        spec.setContent(R.id.test1); 

        spec1.setIndicator("Application"); 
        spec1.setContent(R.id.test2); 

        tabHost.setOnTabChangedListener(this); 
        tabHost.addTab(spec); 
        tabHost.addTab(spec1); 
        return rootView; 
       } 

     @Override 
      public void onTabChanged(String tabId) { 
       // TODO Auto-generated method stub 
       if ("tagApp".equals(tabId)) { 

        Intent intent = new Intent(getActivity(), Test1.class); 
        getActivity().startActivity(intent); 
       } 
       if ("tagApplication".equals(tabId)) { 
        Intent intent = new Intent(getActivity(), Test2.class); 
        getActivity().startActivity(intent); 
       } 
      } 
      } 

    Test1.java file, 
    public class Test1 extends Activity { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.test1); 
     } 
    } 

    Test2.java file, 
    public class Test2 extends Activity { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.test2); 
     } 
    } 

    And call_fragment.xml file is, 

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

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

       <TabWidget 
        android:id="@android:id/tabs" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" /> 

       <FrameLayout 
        android:id="@android:id/tabcontent" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 
       <include layout="@layout/test1" /> 

       <include layout="@layout/test2" /> 
      </FrameLayout> 
      </LinearLayout> 

     </TabHost> 

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

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Tag 1" /> 

    </LinearLayout> 

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

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Tag 2" /> 

    </LinearLayout> 
+0

你的問題是相當不明確的恕我直言。 我的理解是:當某個選項卡被選中/激活時,你想開始另一項活動(如重定向),是否正確? – MABVT 2014-10-29 07:37:00

+0

是的,我想每次點擊所選標籤時重定向。 – Android 2014-10-29 07:41:25

+0

我一個使用此代碼,它是開放的新的活動,但它是它使用什麼,然後在片段打開?@覆蓋 \t公共無效onTabChanged(字符串tabId){ \t \t // TODO自動生成方法存根 \t \t if(「tagApp」.equals(tabId)){ \t \t \t Intent intent = new Intent(getActivity(),Test1.class); \t \t \t getActivity()。startActivity(intent); \t \t} \t \t如果( 「tagApplication」 .equals(tabId)){ \t \t \t意圖意圖=新意圖(getActivity(),Test2.class); \t \t \t getActivity()。startActivity(intent); \t \t} \t} – Android 2014-10-29 09:46:34

回答

0

這是不可能的,如果類型的返回值「視圖」有望迴歸的意圖。 以不同的方式處理這個問題。

對於重定向選項卡,返回空視圖return new View(/* whatever parameters you want to set */);

然後在TabHost中註冊一個onTabChangeListener,然後允許對選項卡做出反應。 對於重定向選項卡索引,請創建並啓動您的Intent,以便啓動新的活動。

對於現有示例,請參閱:How to use TabHost.OnTabChangeListener in android?