Inflate a new xml layout for each view - instantiateItem in the PagerAdapter

So basically I want to populate the pages inside the ViewPager with separate XML layouts for each view position. I am currently doing this with

@Override public Object instantiateItem(View container, int position) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE); if(position == 0){ view = inflater.inflate(R.layout.main, null); ((ViewPager) container).addView(view, 0); } if(position == 1){ view = inflater.inflate(R.layout.main_second, null); ((ViewPager) container).addView(view, 0); } if(position == 2){ view = inflater.inflate(R.layout.main_third, null); ((ViewPager) container).addView(view, 0); } return view; } 

The correct views are displayed first, but when I scroll through the ViewPager, the layout is hidden / destroyed. Why is this? Am I doing it wrong? please help and correct me.

Thanks. I love you. Wesley

+6
source share
1 answer

Check out this link for a good tutorial on how to put it all together. Basically, the idea is that you should add a recently overpriced view to the ViewPager collection:

 View view = inflater.inflate(resId, null); ((ViewPager) collection).addView(view, 0); 
+14
source

Source: https://habr.com/ru/post/918114/


All Articles