/** * Value to indicate that the default caching mechanism of RecyclerView should be used instead * of explicitly prefetch and retain pages to either side of the current page. * @see #setOffscreenPageLimit(int) */ publicstaticfinalint OFFSCREEN_PAGE_LIMIT_DEFAULT = -1;
/** * <p>Set the number of pages that should be retained to either side of the currently visible * page(s). Pages beyond this limit will be recreated from the adapter when needed. Set this to * {@link #OFFSCREEN_PAGE_LIMIT_DEFAULT} to use RecyclerView's caching strategy. The given value * must either be larger than 0, or {@code #OFFSCREEN_PAGE_LIMIT_DEFAULT}.</p> * * <p>Pages within {@code limit} pages away from the current page are created and added to the * view hierarchy, even though they are not visible on the screen. Pages outside this limit will * be removed from the view hierarchy, but the {@code ViewHolder}s will be recycled as usual by * {@link RecyclerView}.</p> * * <p>This is offered as an optimization. If you know in advance the number of pages you will * need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting * can have benefits in perceived smoothness of paging animations and interaction. If you have a * small number of pages (3-4) that you can keep active all at once, less time will be spent in * layout for newly created view subtrees as the user pages back and forth.</p> * * <p>You should keep this limit low, especially if your pages have complex layouts. By default * it is set to {@code OFFSCREEN_PAGE_LIMIT_DEFAULT}.</p> * * @param limit How many pages will be kept offscreen on either side. Valid values are all * values {@code >= 1} and {@link #OFFSCREEN_PAGE_LIMIT_DEFAULT} * @throws IllegalArgumentException If the given limit is invalid * @see #getOffscreenPageLimit() */ publicvoidsetOffscreenPageLimit(@OffscreenPageLimitint limit){ if (limit < 1 && limit != OFFSCREEN_PAGE_LIMIT_DEFAULT) { thrownew IllegalArgumentException( "Offscreen page limit must be OFFSCREEN_PAGE_LIMIT_DEFAULT or a number > 0"); } mOffscreenPageLimit = limit; // Trigger layout so prefetch happens through getExtraLayoutSize() mRecyclerView.requestLayout(); }
viewPager2.apply { offscreenPageLimit=1 val recyclerView= getChildAt(0) as RecyclerView recyclerView.apply { val padding = resources.getDimensionPixelOffset(R.dimen.dp_10) + resources.getDimensionPixelOffset(R.dimen.dp_10) // setting padding on inner RecyclerView puts overscroll effect in the right place setPadding(padding, 0, padding, 0) clipToPadding = false } } val compositePageTransformer = CompositePageTransformer() compositePageTransformer.addTransformer(ScaleInTransformer()) compositePageTransformer.addTransformer(MarginPageTransformer(resources.getDimension(R.dimen.dp_10).toInt())) viewPager2.setPageTransformer(compositePageTransformer)
TabLayoutMediator的构造方法接收三个参数,第一个参数为TabLayout;第二个参数为ViewPager2;第三个参数是TabConfigurationStrategy,这是一个接口,该接口中有一个方法onConfigureTab(@NonNull TabLayout.Tab tab, int position),第一个参数是当前Tab,第二个当前position,源码如下:
1 2 3 4 5 6 7 8 9 10 11
publicinterfaceTabConfigurationStrategy{ /** * Called to configure the tab for the page at the specified position. Typically calls {@link * TabLayout.Tab#setText(CharSequence)}, but any form of styling can be applied. * * @param tab The Tab which should be configured to represent the title of the item at the given * position in the data set. * @param position The position of the item within the adapter's data set. */ void onConfigureTab(@NonNull TabLayout.Tab tab, int position); }