Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not able to get the current view in Viewpager

Tags:

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?

like image 860
user1938357 Avatar asked May 02 '13 06:05

user1938357


People also ask

How do I get ViewPager current position?

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.

Is ViewPager deprecated?

This function is deprecated.

How do I view images in ViewPager Android?

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.

What is difference between ViewPager and ViewPager 2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager .


2 Answers

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());
like image 52
Raj008 Avatar answered Sep 30 '22 08:09

Raj008


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;
    }
like image 22
ultraon Avatar answered Sep 30 '22 10:09

ultraon