11

I have a ViewPager with some fragments. Each fragment has a ListView in a SlidingDrawer (=invisible before swiping) with an 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.

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

user
  • 85,380
  • 17
  • 189
  • 186
metinkale38
  • 678
  • 3
  • 9
  • 26

2 Answers2

16

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

Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
Codedroid
  • 478
  • 7
  • 14
3

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

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

user
  • 85,380
  • 17
  • 189
  • 186
  • i commented the setAdapter call and it was smoother. onPageSelected is a a good idea but i do only get the position number, how to get the current instance of Fragment – metinkale38 May 20 '13 at 08:55
  • @metinkale38 It depends on what type of adapter you use, see the top two answers in this question http://stackoverflow.com/questions/7379165/update-data-in-listfragment-as-part-of-viewpager?answertab=votes#tab-top – user May 20 '13 at 09:04
  • thank you OnPageChangeListener is a good idea, its smoother than before, if i do not swipe 2 Pages immediately after each other. On your link i found this link: http://tamsler.blogspot.nl/2011/11/android-viewpager-and-fragments-part-ii.html and i used the 2nd method to get the Fragment instance – metinkale38 May 20 '13 at 15:24