When a page is selected, I want to display an image with a description in the current page.
Specifically, in the activity
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
viewPagerAdapter.showImage(position);
}
});
In adapter
public void showImage(int position) {
ViewHolder holer = getHolder(position);
holer.imageview.setVisibility(View.VISIBLE);
}
I want the same function as above...
Is there a good solution?
ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .
ViewPager2 uses FragmentStateAdapter objects as a supply for new pages to display, so the FragmentStateAdapter will use the fragment class that you created earlier. Create an activity that does the following things: Sets the content view to be the layout with the ViewPager2 .
You can do something like this
(viewPager2.get(0) as RecyclerView).findViewHolderForAdapterPosition(position)
or
(viewPager2.get(0) as RecyclerView).layoutManager.findViewByPosition(position)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With