如何使用onTabSelected更改标签?

时间:2019-10-31 16:27:30

标签: android android-fragments android-tabs

我开发了一个包含多个页面的应用程序。我没有使用andorid标签页浏览这些页面。

现在,我想在应用程序中添加TabLayout。

我遇到了这个功能。

navigationtabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition())
        {
            case 0:
                Intent myIntent = new Intent(JobSelection.this, RouteSelection.class);
                JobSelection.this.startActivity(myIntent);
                break;
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
        }
    }

尽管导航部分起作用,但片段或选项卡仍然不起作用。

我的问题是如何实际更改片段或选项卡。它确实会转到其他页面,但是仍然在导航栏中选择了错误的选项卡。

这是XML文件上的代码。

<com.google.android.material.tabs.TabLayout
    android:layout_width="381dp"
    android:layout_height="37dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    android:id="@+id/navigationtabs">

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Route"
        android:id="@+id/tabroute"/>

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Job"
        android:id="@+id/tabjob"/>

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pic"
        android:id="@+id/tabpic"/>

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List"
        android:id="@+id/tablist"/>


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

0 个答案:

没有答案