Alex Rivera | Logout

android load data asynchronously in view pager

Asked 2012-05-22T13:30:48.213
9

I need a solution for view pager implementation. Firstly I am loading huge data from database in single page,so sometimes during swipe it slows down swipe frequency(you need to multiple time swipe on page) as in background it is doing fetching task. I am not using async task for returning view. Is there any way to lazy load pages just allow user to go on other page on swipe but data is lazy loaded.

My code sample is as below;

 public Object instantiateItem(View container, int position) {

View v;

v = View.inflate(context,R.layout.swipearea, null);

listView = (ListView)v.findViewById(R.id.MyListView);
largeDataCall();
((ViewPager)container).addView(v);
return v;
}

I am calling this in on create method.

pager=(ViewPager) findViewById(R.id.pagerAdapter);
pager.setAdapter(new SimplePager(MyPager.this));
pager.setCurrentItem(364);

Is there any solution?

Edit
Report

1 Answer

1

I had a similar problem. A viewpager which was loading heavy data. If you are not going to change the views of the individual pages often, then I would suggest you keep the pages in memory. Use following code to perform this

mViewPager.setOffscreenPageLimit(#pages); to keep #pages in memory. I had 5 pages so my #pages was 4. 

If you want to refresh data on the viewpager slides, use

mViewPager.getAdapter().notifyDataSetChanged(); with getItemPosition() returning POSITION_NONE.

And use FragmentStatePagerAdapter.

answered 2013-01-30T16:18:13.653

Your Answer