2017-10-09 30 views
0

我有選項卡,並且想要在單擊它時移動到不同的活動。它根本沒有工作。也許,它有不同的配置,因爲它只是一個tabItem而不是Button。但是,我不知道這件事。使用tabItem無法移動到其他活動

有人可以幫助我嗎?

1)activity_main.xml中

<android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:color/background_light" 
      app:tabMode="scrollable"> 

      <android.support.design.widget.TabItem 
       android:id="@+id/hotel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:icon="@drawable/nav_hotel" 
       android:text="Hotel" /> 

      <android.support.design.widget.TabItem 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:icon="@drawable/nav_flight" 
       android:text="Pesawat" /> 

      <android.support.design.widget.TabItem 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:icon="@drawable/nav_train" 
       android:text="Kereta" /> 

      <android.support.design.widget.TabItem 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:icon="@drawable/nav_event" 
       android:text="Event" /> 

      <android.support.design.widget.TabItem 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:icon="@drawable/nav_car" 
       android:text="Mobil" /> 
     </android.support.design.widget.TabLayout> 
    </android.support.design.widget.AppBarLayout> 

2)MainActivity.java

TabItem tabItem = (TabItem) findViewById(R.id.hotel); 
tabItem.setOnClickListener(new View.OnClickListener() {   
     public void onClick(View v) {    
      Intent fp = new Intent(MainActivity.this, AnotherActivity.class);    
      startActivity(fp);    
     } 
}); 
+0

任何更新???? –

+0

亞..它正在工作。我認爲你需要指定tabItem的來源。這對其他與我有同樣問題的人會很有用。謝謝。 – Rido

回答

1

嘗試此

tabItem.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       Intent fp = new Intent(MainActivity.this, AnotherActivity.class);    
       startActivity(fp); 
      } 

      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 

      } 

      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 

      } 
     }); 
相關問題