I am using a viewpager to display some text across multiple pages. I am trying to get the current view an I select a new page. The reason I want to get the current view is because based on the page number I am changing some properties of some view components like textview, image etc. I am using the below code for the same
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
View CurrView;
OnPageChangeListener pageChangelistener = new OnPageChangeListener() {
@Override
public void onPageSelected(int pageSelected) {
currView = mPager.getChildAt(mPager.getCurrentItem());
myTextView = (TextView)currView.findViewById(R.id.myTextView);
loadBookmarkSetting(pageSelected);//do some changes in myTextView properties based on the page number }
mPager.setOnPageChangeListener(pageChangelistener);
Now the problem I am facing that after I scroll 3 pages and when I come to 4th page, i get nullpointer exception on the line "myTextView = (TextView)currView.findViewById(R.id.myTextView);". This is so becuase currView is null. Any ideas if I am doing something wrong here. Is there any other way to capture current view(page) in viewpager so as I can do changes on the view based on what page number I am on?
getChildAt(1); will return the current page. But, if you then change back to page 2 (from page 3) your list of children will be in this order page 2, page 3, page 1 which means that ViewPager. getChildAt(1); does not return the current page.
This function is deprecated.
Java. After creating the Adapter for the ViewPager, reference the ViewPager from the XML and set the adapter to it in the MainActivity. java file. Create an array of integer which contains the images which we will show in the ViewPager.
ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager .
You can use the setTag with the view you are returning in your instantiateItem method(PagerAdapter).
So, when you are instantiating your views in PagerAdapter the method is also instantiateItem(ViewGroup container, int position))
, you should use setTag to your view.
Like this:
view.setTag("myview" + position);
return view;
when you need the current view:
View view = (View) pager.findViewWithTag("myview" + pager.getCurrentItem());
You use this approach:
@Nullable
public static View getActiveView(@Nonnull final ViewPager viewPager) {
final PagerAdapter adapter = viewPager.getAdapter();
if (null == adapter || adapter.getCount() == 0 || viewPager.getChildCount() == 0) {
return null;
}
int position;
final int currentPosition = viewPager.getCurrentItem();
for (int i = 0; i < viewPager.getChildCount(); i++) {
final View child = viewPager.getChildAt(i);
final LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
if (layoutParams.isDecor) {
continue;
}
final Field positionField;
try {
positionField = LayoutParams.class.getDeclaredField("position");
positionField.setAccessible(true);
position = positionField.getInt(layoutParams);
} catch (NoSuchFieldException e) {
break;
} catch (IllegalAccessException e) {
break;
}
if (position == currentPosition) {
return child;
}
}
return null;
}
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