简体   繁体   English

Fragment中的ViewPager + Adapter => laggy swiping

[英]ViewPager + Adapter in Fragment => laggy swiping

I have a ViewPager with some fragments. 我有一个带有一些片段的ViewPager Each fragment has a ListView in a SlidingDrawer (=invisible before swiping) with an ArrayAdapter . 每个片段在SlidingDrawer都有一个ListView (在滑动前不可见)和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 ViewPager is idle? 我的问题是,当ViewPager闲置时,是否可以在刷卡后设置适配器? Or is there a better way? 或者,还有更好的方法? The List needs to be already loaded when the SlidingDrawer is expanded. 在展开SlidingDrawer时,需要已经加载List。

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方面确实没有给出糟糕的滑动体验......所以,想法是在加载之前在线程中等待400ms列出并确保当用户尝试快速滑动以查看viewpager内容时,您实际上不加载内容...

My Question is, wether it is possible to set the Adapter after swiping when it Pager is idle? 我的问题是,当寻呼机空闲时刷卡后可以设置适配器吗?

There is the OnPageChangeListener that you could set on the ViewPager to monitor the swipe gestures. 您可以在ViewPager上设置OnPageChangeListener来监控滑动手势。 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. 然后,您可以使用onPageSelected() (或onPageScrollStateChanged()来监视当前状态)方法,以便在选择新页面时收到通知,并从该方法开始加载数据。

Also, make sure the ListView are responsible for the lag and not some other part of your code. 此外,请确保ListView负责滞后而不是代码的其他部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM