3

Hello I am using the View Pager and PagerAdapter. In PagerAdapter 'instantiateItem()' is calling two times initially.

Please help me to solve this such the instantiateItem() should call only one time intial also.

Thanks in advance.

public Object instantiateItem(View collection, int position) {
    LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    int resId = 0;
    switch (position) {
        case 0: {
            resId = R.layout.first_template;
            break;
        }
        case 1: {
            resId = R.layout.second_template;
            break;
        }
        case 2: {
            resId = R.layout.third_template;
            break;
        }
        case 3: 
            resId = R.layout.first_template;
            break;
    }
    View view = inflater.inflate(resId, null);
    ((ViewPager)collection).addView(view, 0);
    return view;
}

@Override
public void destroyItem(View view, int arg1, Object arg2) {
    ((ViewPager) view).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View view, Object obj) {
    return view == ((View) obj);

}

@Override
public Parcelable saveState() {
    return null;
}

@Override
public int getCount() {
    return 4;
}

public class PageViewActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_view);
        PageAdapter adapter = new PageAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }
}
  • Is it being called two times with the same position? – DavidAndroidDev Jun 14 '13 at 11:41
  • Initially its zero and then one.. This called two times only initially then after it call only one time. – user2453067 Jun 14 '13 at 11:41
  • 1
    That's the way the view pager is built. It will call the second layout so that way when you swipe or transition to the next view, Android doesn't have to build. You'll notice that once you swipe to the 2nd view, Android will make a call to the third position once the 2nd view is in view. – DavidAndroidDev Jun 14 '13 at 11:43
  • Actually it called the first and second initially without do any swipe on there. – user2453067 Jun 14 '13 at 11:44
  • Right, because Android is default set to load 1 offscreen page. The initial page is 0, but the view pager is set to load the next offscreen one also. Check my answer to see if that fixes the problem for you. – DavidAndroidDev Jun 14 '13 at 11:46
  • Although I should add, this is meant more as an optimization for your user. With the view already loaded, the user shouldn't see any delay in swiping. By disabling the offscreen paging, you might end up with a choppier transition depending on how complex your layout is. – DavidAndroidDev Jun 14 '13 at 11:48
  • @DavidAndroidDev See my last comment in your answer – user2453067 Jun 14 '13 at 11:49
  • Well, my question is, how is this causing a problem in your code? The `ViewPager` is working as its supposed to. Is there another problem that is happening that you haven't covered here? – DavidAndroidDev Jun 14 '13 at 11:55
  • How here I get the reference of view correctly because its getting called two times. – user2453067 Jun 14 '13 at 12:54
  • Have you got any solution for this. i am also stuck over here @user2453067 – Akarsh M Feb 07 '14 at 12:00
  • Have you any solution for this. i am also stuck over here. @DavidAndroidDev – Akarsh M Feb 07 '14 at 12:01

0 Answers0