在我的预测应用中,我每天都会实现一个片段。
架构是: FragmentActivity<< PagerTabStrip + ViewPager<< FragmentPagerAdapter
一切正常。 我需要根据它的可见性以这种方式显示片段标题:
01/10/16 | 2016年10月2日| 16年3月10日
我的第一个想法是在FragmentPagerAdapter中执行:
public void set_current_position(int i)
{
mCurrent_position = i;
}
@Override
public CharSequence getPageTitle(int position)
{
String strDT = null;
boolean isTop = (position == mCurrent_position);
Fragment fragment = (Fragment) getItem(position);
Bundle data = fragment.getArguments();
long ts = data.getLong(keyDate);
Date date = new Date(ts);
if (isTop)
strDT = FullDateFormat(date);
else
strDT = ShortDateFormat(date);
return strDT;
}
结果不是恒定的;有时候它工作得很好但有时候没有。
我认为set_current_position
是在'getPageTitle
之前或之后随机执行的。
你有什么想法吗?
答案 0 :(得分:0)
您可以尝试将OnTabSelectedListener设置为TabLayout。
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
// do your stuff
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});