使用tabItem无法移动到其他活动
问题描述:
我有选项卡,并且想要在单击它时移动到不同的活动。它根本没有工作。也许,它有不同的配置,因为它只是一个tabItem而不是Button。但是,我不知道这件事。使用tabItem无法移动到其他活动
有人可以帮助我吗?
1)activity_main.xml中
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
app:tabMode="scrollable">
<android.support.design.widget.TabItem
android:id="@+id/hotel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/nav_hotel"
android:text="Hotel" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/nav_flight"
android:text="Pesawat" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/nav_train"
android:text="Kereta" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/nav_event"
android:text="Event" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/nav_car"
android:text="Mobil" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
2)MainActivity.java
TabItem tabItem = (TabItem) findViewById(R.id.hotel);
tabItem.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent fp = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(fp);
}
});
答
尝试此
tabItem.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Intent fp = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(fp);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
任何更新???? –
亚..它正在工作。我认为你需要指定tabItem的来源。这对其他与我有同样问题的人会很有用。谢谢。 – Rido