First viewpager page blank

余生颓废 提交于 2019-12-05 17:01:45

I had the same issue with ViewPagers, I used them with Fragments. The fragment inside first page was always empty second and later pages were fine and first pages was ok when i returned from 2nd or 3rd page.

I found out the reason was that I loaded the data from database in onCreateView method of the fragment, Views were being created before data could be loaded inside them from database. I changed my code to load data in onStart method of the fragment and everything was fine.

@Override
public void onStart() {
   getActivity().getLoaderManager().initLoader(LOADER_ID,null, this);
   super.onStart();
}

Hope this helps.

The issue must be because card is taking time to load data from database and view is not updated after data is being loaded.Try adding

 handler.postDelayed(r=new Runnable()
        {
            @Override
            public void run() {
                if (mPosition == position)
                {

                    notifyDataSetChanged();
                   handler.removeCallbacks(r);


                }
            }
        }, 1000);

inside instantiateItem of adapter so that view is updated after 1 second when it is being created.mPosition is the postion of the first card that is being opened.If you are opening viewpager by clicking on item in list then you need to pass the postion of the item that is being selected when calling adapter..else u can set mPostion as 0 if the first item is always initially opened in viewpager

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