Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager2, get current page viewholder

Tags:

android

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?

like image 229
and.s.s Avatar asked Jan 17 '20 03:01

and.s.s


People also ask

What is difference between ViewPager and ViewPager2?

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 .

What is ViewPager2 in android?

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 .


1 Answers

You can do something like this

(viewPager2.get(0) as RecyclerView).findViewHolderForAdapterPosition(position)

or

(viewPager2.get(0) as RecyclerView).layoutManager.findViewByPosition(position)
like image 153
dinis Avatar answered Oct 02 '22 11:10

dinis