ViewPager is used for projects over the past few days, because it can achieve the effect of moving multiple pages left and right, and then
When ListView is used on each page, "PagerAdapter java. lang. IllegalStateException" is always displayed during runtime:
The specified child already has a parent. You must call removeView ()... "causes The program to end.
Displayed on PagerAdapter during debugging:
@ Override
Public Object instantiateItem (ViewGroup container, int position)
{
Container. addView (views. get (position ));
Return views. get (position );
}
The red code is incorrect. It is hard to understand that the newly added view will automatically bind a parent class. Because a son view cannot be related to two parent classes
You must unbind it.
A lot of solutions have been searched on the internet, mostly using mViewPager. setOffscreenPageLimit (views. size (); this method does not need to be determined
Whether the parent already exists, but the redundant listview still cannot be destroy.
I am going to post my solution to the problem. I hope that you don't have to worry about it:
@ Override
Public Object instantiateItem (ViewGroup container, int position)
{
Try {
If (views. get (position). getParent () = null ){
Container. addView (views. get (position ));
} Else {
(ViewGroup) views. get (position). getParent (). removeView (views. get (position ));
Container. addView (views. get (position ));
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return views. get (position );
}