在onCreate
中,我使用addOnTabSelectedListener
添加了一个监听器。当初始索引为0时,似乎不会调用onTabSelected
。如何才能获得此“选定0”事件?
以下是发生的事情的时间表。大胆的部分是问题。我需要收到选项卡0的通知。
代码
myAdapter = MyTabAdapter(supportFragmentManager);
viewPager.adapter = myAdapter;
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
布局
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
app:tabMode="fixed"
app:tabGravity="fill"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</android.support.v4.view.ViewPager>
答案 0 :(得分:1)
手动调用
TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
@Override public void onTabSelected(TabLayout.Tab tab) {
}
@Override public void onTabUnselected(TabLayout.Tab tab) {
}
@Override public void onTabReselected(TabLayout.Tab tab) {
}
};
onTabSelectedListener.onTabSelected(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()));
tabLayout.addOnTabSelectedListener(onTabSelectedListener);