背景:我有一个简单的Activity
,ViewPager
使用FragmentStatePagerAdapter
适配器。适配器提供5个碎片。在ViewPager
的每一页上,我都有一个简单的Fragment
,它只包含一个不确定的(循环)ProgressBar
。我还在顶部有一个自定义选项卡视图,可以轻松切换标签/片段。
问题是,如果我启动活动并导航到Tab 3,4或5(对于未缓存的Tab),则冻结ProgressBar
,它不是动画。
通知:据我所知,如果getOffscreenPageLimit
涵盖了所有可能的标签(在我的情况下,如果getOffscreenPageLimit() == 4
)已加载,则问题永远不会发生。问题只发生在我切换到需要在移动时呈现的标签时:所以如果我使用默认的OffscreenPageLimit
那么前两个标签是好的,但不是第三个标签。
Activity:
public class SpinnerTest extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
ViewPager mPager = (ViewPager) findViewById(R.id.viewpager);
mPager.setAdapter(adapter);
// setting up custom tabview
SlidingTabLayout tabs = ButterKnife.findById(this, R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return ContextCompat.getColor(SpinnerTest.this, R.color.colorAccent);
}
@Override
public int getDividerColor(int position) {
return ContextCompat.getColor(SpinnerTest.this, android.R.color.transparent);
}
});
tabs.setViewPager(mPager);
}
private static class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new SpinnerFragment();
}
@Override
public CharSequence getPageTitle(int position) {
return "Tab" + (position + 1);
}
@Override
public int getCount() {
return 5;
}
}
}
activity_spinner.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
Fragment:
public class SpinnerFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_spinner, container, false);
}
}
fragment_spinner.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>