2017-02-26 104 views
0

TabLayoutに複数のタブがあります。 xml:Android TabLayout Clickイベント

 <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     app:tabMode="scrollable"> 

     <android.support.design.widget.TabItem 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:id="@+id/info_tab" 
      android:icon="@drawable/info" 
      android:clickable="true"/> 
... 

これらのためにOnClickListenerを設定する方法と非常に混同しています。すべてのオンラインソースは、私がTabHostを使用していると仮定していますが、それらの関数はTabLayoutには適用されません。代わりにTabHostを使うべきですか?または、TabLayoutのOnClickListenerの外観はどうでしょうか?

ありがとうございます!

答えて

0

あなたがこれを使用することができます...

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 


     } 

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

     } 

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

     } 
    }); 

とonTabSelectedに、あなたは彼がそのタブ名をクリックすると、ユーザはそのフラグメントに行かせるためにコードを書くことができます。.. :)

0
using MyTabLayout = Android.Support.Design.Widget.TabLayout; 
protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     SetContentView(Resource.Layout.Main);    
     var tabLayout = FindViewById<MyTabLayout>(Resource.Id.sliding_tabs); 
     tabLayout.AddTab(tabLayout.NewTab().SetText("CNN")); 
     tabLayout.AddTab(tabLayout.NewTab().SetText("BBC")); 
     tabLayout.AddTab(tabLayout.NewTab().SetText("ABC7News")); 
     tabLayout.TabGravity = MyTabLayout.GravityFill; 
     tabLayout.TabSelected += TabLayout_TabSelected; 
    } 



    private void TabLayout_TabSelected(object sender, MyTabLayout.TabSelectedEventArgs e) 
    { 
     switch (e.Tab.Position) 
     { 
      case 0: ShowAlert("0"); break; 
      case 1: ShowAlert("1"); break; 
      case 2: ShowAlert("2"); break; 
     } 
    } 

sliding_tabs - <android.support.design.widget.TabLayout />