API 30 Android 10.0+ (Google API), AVD (x86)
Проблема в том ... Просто для проверки ViewPager2. Я использовал ViewPager2 с TabLayout и прикрепленными фрагментами. Затем я установил «предельное значение страницы вне экрана» на 1. Я ожидал, что будет сохранено 3 страницы. (текущая, левая, правая страница) Но сохраняется около 6 страниц. Когда я использую предыдущий ViewPager, он работает хорошо.
Я прочитал ... Я прочитал документ на ‹веб-сайте разработчиков Android ›. Но я не могу найти причину вышеуказанной проблемы, и я не знаю, что «OFFSCREEN_PAGE_LIMIT_DEFAULT» в документе означает, сколько страниц нужно поддерживать. Он определяется просто -1.
Код ...
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager2 viewPager;
private ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tabLayout);
viewPager = findViewById(R.id.viewPager);
viewPager.setOffscreenPageLimit(1);
viewPager.setAdapter(new ViewPagerAdapter(this, 9));
new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
@Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText("Tab " + (position + 1));
}
}).attach();
}
}