android TabItem onClick 不起作用

android TabItem onClick doesn't work

我有 cityClick 函数, 如果我从 textView 调用此函数它工作正常,但如果我从 TabItem 调用 cityClick 它不起作用,这是怎么回事?

Java

public class Kategorie extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

protected void cityClick(View view) {
    Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show();
}

布局

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:clickable="true"
        android:onClick="cityClick"
        app:tabMode="fixed">

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:textAlignment="center"
            android:textSize="18sp"
            android:onClick="cityClick"
            android:clickable="true"
            android:text="GDAŃSK" />

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:textAlignment="center"
            android:textSize="18sp"
            android:onClick="cityClick"
            android:clickable="true"
            android:text="SOPOT" />

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:textAlignment="center"
            android:textSize="18sp"
            android:onClick="cityClick"
            android:clickable="true"
            android:text="GDYNIA" />

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

我在这个论坛上搜索,但没有找到任何可以帮助我的东西。

试试这个,

  tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Toast.makeText(mActivity, "hai", Toast.LENGTH_SHORT).show();
            }

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

            }

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

            }
        });

在 Kotlin 中,

tabs.addOnTabSelectedListener(object:TabLayout.OnTabSelectedListener {
    override fun onTabSelected(tab : TabLayout.Tab) {
        Toast.makeText(mActivity, tab.text, Toast.LENGTH_SHORT).show()
    }
    override fun onTabUnselected(p0: TabLayout.Tab?) {

    }
    override fun onTabReselected(p0: TabLayout.Tab?) {

    }
})

由于可能存在多个选项卡,这样会更完整:

TabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
//                        codes related to the first tab
                        break;
                    case 1:
//                        codes related to the second tab
                        break;
                    case 2:
//                        codes related to the third tab
                        break;
                    case 3:
//                        codes related to the fourth tab
                        break;
                        .
                        .
                        .
                }
            }

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

            }

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

            }
        });