java ViewPager + Fragment 中的适配器 => 滑动延迟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16644853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:33:02  来源:igfitidea点击:

ViewPager + Adapter in Fragment => laggy swiping

javaandroidandroid-fragmentsandroid-viewpageradapter

提问by metinkale38

I have a ViewPagerwith some fragments. Each fragment has a ListViewin a SlidingDrawer(=invisible before swiping) with an ArrayAdapter.

我有ViewPager一些碎片。每个片段都有一个ListViewin a SlidingDrawer(=滑动前不可见)和一个ArrayAdapter.

Adapter is set on onCreateView(), that slows down swiping, because 30 list items have to load each time I swipe, because new fragments are being created.

适配器设置为onCreateView(),这会减慢滑动速度,因为每次滑动时必须加载 30 个列表项,因为正在创建新片段。

My Question is, whether it is possible to set the adapter after swiping when it ViewPageris idle? Or is there a better way? The List needs to be already loaded when the SlidingDrawer is expanded.

我的问题是,是否可以在ViewPager空闲时滑动后设置适配器?或者,还有更好的方法?当 SlidingDrawer 展开时,List 需要已经加载。

采纳答案by Luksprog

My Question is, wether it is possible to set the Adapter after swiping when it Pager is idle?

我的问题是,当 Pager 空闲时,是否可以在滑动后设置适配器?

There is the OnPageChangeListenerthat you could set on the ViewPagerto monitor the swipe gestures. You could then use the onPageSelected()(or the onPageScrollStateChanged()to monitor the current state) method to get notified when a new page has been selected and start from that method the loading of data.

还有就是OnPageChangeListener,你可以在设置ViewPager监控的滑动手势。然后,您可以使用onPageSelected()(或onPageScrollStateChanged()监视当前状态)方法在选择新页面时获得通知,并从该方法开始加载数据。

Also, make sure the ListVieware responsible for the lag and not some other part of your code.

另外,请确保ListView是造成延迟的原因,而不是代码的其他部分。

回答by Codedroid

I had a similar problem... I used listeners. Still, when you swipe two pages back to back it was laggy... I did something like this that improved the experience....

我有一个类似的问题......我使用了听众。尽管如此,当你背靠背滑动两页时,它仍然很慢......我做了这样的事情来改善体验......

viewpager.setOnPageChangeListener(new OnPageChangeListener() {
    int positionCurrent;
    boolean dontLoadList;
    @Override
    public void onPageScrollStateChanged(int state) {   
        if(state == 0){ // the viewpager is idle as swipping ended
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if(!dontLoadList){
                        //async thread code to execute loading the list... 
                        }
                    }
                },200);
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        positionCurrent = position; 
        if( positionOffset == 0 && positionOffsetPixels == 0 ) // the offset is zero when the swiping ends{
            dontLoadList = false;
        }
        else
            dontLoadList = true; // To avoid loading content for list after swiping the pager.
    }

}

If you take a few milli seconds to load the list that comes as supplement to the viewpager, its ok in terms of UX rather than giving a bad swiping experience... So, the idea is to wait for 400ms in the thread before loading the list and making sure that you actually dont load content when the user is trying to swipe fast to see the viewpager content...

如果你花几毫秒来加载作为 viewpager 补充的列表,它在 UX 方面还可以,而不是给一个糟糕的滑动体验......所以,这个想法是在加载之前在线程中等待 400 毫秒列出并确保在用户尝试快速滑动以查看 viewpager 内容时实际上不加载内容...