ViewPager + Adapter in Fragment => laggy swiping

前端 未结 2 1740
滥情空心
滥情空心 2020-12-28 20:46

I have a ViewPager with some fragments. Each fragment has a ListView in a SlidingDrawer (=invisible before swiping) with an Arra

2条回答
  •  长情又很酷
    2020-12-28 21:07

    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...

提交回复
热议问题