android - viewPager滑动浏览一组大图,滑动倒数第二张时请求新数据追加数据……
大家讲道理
大家讲道理 2017-04-18 09:16:18
0
5
766
  • 在listView中点击例如“美女图片”后打开一个largeActivity;

  • largeActivity中有一个viewPager实现左右滑动浏览大图;

  • 在largeActivity打开的时候会通过okHttp从服务器请求一组5张图片链接,图片通过glide加载在viewPager中实现左右滑动浏览;

  • 因为每次网络请求只能返回5张图片链接,所以我在监听每次滑动viewPager的时候判断当前 postion小于存储图片链接的imageList.size() -1的时候,也就是每次滑动到倒数第二张的时候去网络请求下一组图片的链接;

  • 然后通过add将新请求到的图片链接添加进imageList,然后再执行一次
    pagerAdapter.notifyDataSetChanged()来实现viewPager继续滑动浏览新请求到的图片。

现在问题是如果用户以正常速度滑动viewPager来浏览每张图片的时候是没有任何问题的,但是如果用户飞速滑动viewPager的进行浏览的时候就会出现在某一次请求新数据后就会出现APP崩溃的情况;错误信息在两部不同的手机上报错不一样,但是都是在相同的情况下崩溃。Google遍都没有找到解决办法

华为荣耀报错如下:

通过华为手机的报错在网上找到通过设置 android:hardwareAccelerated="false"
来关闭硬件加速的办法,但是通过测试在快速滑动时还是会导致APP崩溃自动重启的情况

乐视max2报错如下:

03-21 05:42:44.071 18730-18730/com.abcd.reader.abcdreader E/InputEventReceiver: Exception dispatching input event.
03-21 05:42:44.071 18730-18730/com.abcd.reader.abcdreader E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
03-21 05:42:44.084 18730-18730/com.abcd.reader.abcdreader E/MessageQueue-JNI: java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 25, found: 30 Pager id: com.abcd.reader.abcdreader:id/vp_pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.abcd.reader.abcdreader.adapter.FollowDetailViewPagerAdapter

通过乐视手机的报错怀疑是当新请求的数据add进imageList后,但是还没来得及执行notifyDataSetChanged,刚好在这时用户在滑动viewPager的时候导致的;为了验证猜测我修改了在当前 postion小于存储图片链接的imageList.size() -2的时候,也就是每次滑动到倒数第三张的时候去网络请求下一组图片的链接;给imageList.add和notifyDataSetChanged预留了充分的执行时间,这时候再次测试问题消失,华为手机和乐视手机均没有再出现APP崩溃重启的情况


但是我这个服务器请求回来的数据是不固定的,有时候可能每次返回5条,有时候10条,有时候只有1条,所以我现在的解决办法并不能彻底解决
烦请老师们给予指点一二,万分感谢!

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(5)
PHPzhong

Boldly estimate that it should be a thread safety issue. You can try the following modifications. When obtaining new data, use post方法去做更新,即
viewPager.post()notifyDataSetChanged放到posttask. I haven't personally verified it yet. If it doesn't work, please tell me and I'll take a look.

PHPzhong

I can’t help you locate the specific cause of the error at the moment. I’ll give you another implementation idea. You can try using RecycleView to achieve the same effect.

黄舟

I don’t know if I understood it wrong, but images.size()-1 is not the second to last image, but the last one

It is recommended to write the logic of pulling items from the server to getItem of Adapter;

 @Override
        public Fragment getItem(int position) {
            if(getCount()-1 == position){
                //在此处拉取
            }
            return ArrayListFragment.newInstance(position);
        }
阿神

The strategy is wrong. Why only 5 image URLs are returned each time? Is it to save traffic? The biggest waste is the three-way handshake creation process. You should get all the URLs at the beginning, and then control the image caching yourself.
You don’t have to worry about trouble whenever you go to the second to last one and request it again...

伊谢尔伦

Looking at the question, the subject is making network requests in the viewpager sliding listening method.
If the user quickly slides the viewpager left and right, the callback is likely to be executed multiple times.
The previous network request is still in progress, and the next request will crash.
If this is the reason, you can add a flag. If the current request is in progress, no new network requests will be made until a correct or error callback is received.
As for the problem of a fixed number of returned items, if you request 5 pictures and the server only returns 3, it means that the server has given you all the pictures and your network request should not be executed

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!