Destroy item from the ViewPager's adapter after screen orientation changed

前端 未结 1 1658
夕颜
夕颜 2020-12-29 04:30

So I\'m having a problem with destroying (removing) one page from the ViewPager after the screen orientation changed. I\'ll try to describe the problem in the f

相关标签:
1条回答
  • 2020-12-29 04:53

    UPDATE: SOLUTION FOUND OK, so this took a while. The problem was that the destroyItem() callback was called twice on the progress fragment, once when the screen orientation changed and then once again after the api call finished. That's why the exception. The solution that I found is the following: Keep tracking if the api call finished or not and destroy the progress fragment just in this case, code below.

    @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                if (object.equals(progressElement) && apiCallFinished == true) {
                    apiCallFinished = false;
                    currentFragments.put(position, currentFragments.get(position + 1));
                    super.destroyItem(container, position, object);
                } else if (!(object.equals(progressElement))) {
                    currentFragments.put(position, null);
                    super.destroyItem(container, position, object);
                }
            }
    

    and then this apiCallFinished is set to false in the constructor of the adapter and to true in the onTaskSuccess() callback. And it really works!

    0 讨论(0)
提交回复
热议问题