ViewPager works Fine at first time but on reloading again, getting the error java.lang.IllegalStateException?

前端 未结 2 1310
时光说笑
时光说笑 2020-12-16 00:56

I have a ViewPager with the 10 images comes through webservices(JSON), At first ViewPager work smoothly (fine).

but When back from the activity and reopen it

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 01:48

    Reason of the problem the Pager can be refreshed during some measurement and in this case will refresh count of your adapter, if this count will not equals with saved count you will see the error. So my solution looks:

    public abstract class ViewPagerCursorAdapter extends PagerAdapter {
    
        private int mCount;
    
        public ViewPagerCursorAdapter(Context ctx, Cursor cursor, int resource) {
            super();
            ...
            mCount = cursor.getCount();
            ...
        }
    
        @Override
        public int getCount() {
            return mCount;
        }
    
        public Cursor swapCursor(Cursor newCursor) {
            ...
            mCount = newCursor.getCount();
            notifyDataSetChanged();
        }
    }
    

    Good luck.

提交回复
热议问题