App crashes when calling setCurrentItem to newly added view to adapter.
Hi, Thank you so much for this wonderful library. It is very useful and has helped me immensely.
But I found an issue with using setCurrentItem on my viewPager. Here is the flow :
I have initialized the ViewPager, PagerAdapter and InkPageIndicator testAdapter = new SimplePagerAdapter(testList); testPager.setAdapter(testAdapter); testIndicator.setViewPager(testPager);
Then I add few views to testList and call notifyDataSetChanged on testAdapter. (Which internally calls setPageCount and pageCount gets initialized correctly, which internally calls requestLayout , which would be called on next idle cycle) testList.add(newView); testPager.getAdapter().notifyDataSetChanged();
Then I immediately call setCurrentItem(newViewIndex, false); which calls setPageSelected inline which calls setSelectedPage. setSelectedPage tries to call createMoveSelectedAnimator but dotCenterX doesn't contain newly added view's dot's X value. This is because calculateDotPositions isn't called as yet since onMeasure is not called as yet and is waiting to be called in idle cycle.
Could you please give me a suggestion on how can I fix this. I would create the patch and create pull request for the same.
this happens for a fragment restore too.
Could you guys provide a sample with the problem ? I think it's easy to fix the problem that way
Try viewpager.setCurrentItem() in thread because this helped for me:
Thread td = new Thread() { @Override public void run() { super.run(); try { sleep(500); } catch (Exception e) { e.printStackTrace(); } finally { viewPager.setCurrentItem(indexNo); } }
};td.start();