Multiple ViewPagers Inside Single ListView is Crashing

随声附和 提交于 2019-12-02 01:03:01

Ok so after 2 days, I was able to solve a similar issue like this.

First do not persist your ViewPager in the getView(). What I mean by this is that you shouldn't call convertView.setTag(holder); or holder = (ViewHolder)convertView.getTag(). Just remove the if statement that checks if convertView is null. Then completely remove the else statement block.

Another change to make (this is not one of the causes) is you need to optimize the way you set the Ids. Instead of setId(position+1), make an Ids.xml file in your values folder, then inside that file, put this:

<?xml version="1.0" encoding="utf-8"?>

<resources>
  <!-- use to generate non-conflicting ids for the viewpager -->

  <item name="viewpager_id_base" type="id"/>
</resources>

Then in your getView() method replace setId(position+1) with setId(R.id.viewpager_id_base + position)

After the above change, the list will not crash, but it will disappear after you scroll the item off screen. Here is the fix for that:

Make sure your Custom PageAdapter extends FragmentStatePagerAdapter, not FragmentPagerAdapter. This will properly destroy the views and trigger the onCreateView().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!