Android:TabLayout 当中的tabItem点击事件

本篇文章只含有tabItem的点击事件,其他没有滑动等内容,赶时间及时略过该文

在这里插入图片描述

布局

    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabRippleColor = "@android:color/transparent"
        android:id="@+id/tab_tablayout"
        >

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tab_event_details"
            android:text="事件信息" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tab_deal_details"
            android:text="处理信息" />

    </com.google.android.material.tabs.TabLayout>

代码

        tab_tablayout =  (TabLayout) findViewById(R.id.tab_tablayout);
        tab_tablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

              //  tab.getPosition()  返回数字,从0开始
               // tab.getText()  返回字符串类型,从0开始
                if (tab.getPosition()==0){
                    sv_eventdetail.setVisibility(View.VISIBLE);//这个是用来显示隐藏的布局用的
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

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

            }
        });

还要注意一下定义

private TabLayout tab_tablayout;

猜你喜欢

转载自blog.csdn.net/title71/article/details/113175593