【问题标题】:Android TabLayout Click EventsAndroid TabLayout 点击事件
【发布时间】:2017-02-26 00:02:53
【问题描述】:

我在 TabLayout 中有几个选项卡。 xml:

      <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 是什么样的?

谢谢!

【问题讨论】:

    标签: java android xml mobile


    【解决方案1】:

    你可以用这个...

    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 中,您可以编写代码以使用户在单击该选项卡名称时转到该片段..:)

    【讨论】:

      【解决方案2】:
      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 - &lt;android.support.design.widget.TabLayout /&gt;

      【讨论】:

        猜你喜欢
        • 2013-03-19
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-26
        • 1970-01-01
        相关资源
        最近更新 更多