Android data loading asynchronously in the field of view of a pager

I need a solution to implement a pager view. Firstly, I load huge data from the database on one page, so sometimes during the swipe it slows down the scroll frequency (you need to scroll the page multiple times), since it performs a task in the background. I do not use the async task to return the view. Is there a way for lazy loading pages so that the user can go to another page while scrolling, but the data is lazy loaded.

My sample code is as follows:

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 call this in the create method.

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

Is there any solution?

+8
android android-asynctask android-viewpager
source share
6 answers

I suggest working with fragments (and not directly with views).

You need an interface on your snippets to tell them when they will be shown:

 public interface IShowedFragment { public void onShowedFragment(); } 

Make all your fragments implemented in this interface, and in this method call the loaders / asyncTasks / background tasks.

Then add onPageChangeListener to your ViewPager, and when you find that the user has changed the page, call the interface method on your fragment. You have a choice with this listener, with one of the methods that you can wait for the viewPager to stop sliding to invoke the interface call.

To get the correct fragment to make this call, take the fragment from your FragmentApadter.instantiateItem (ViewGroup, int), which will return the fragment for this position if it is already loaded.

 mPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { Fragment fragment = (Fragment) mAdapter.instantiateItem(mPager, position); if(fragment instanceof IShowedFragment){ ((IShowedFragment) fragment).onShowedFragment(); } } (...) 

Just as you can prepare your fragments with empty views, and when you click on one, you will start loading the data.

+17
source share

I just completed a very similar task. To start your search for a solution to your problem, consider the following points:

  • See if you need to retrieve all this data in the first instance. Feel free to post messages with some details about what information you need to download and what you do with it (display it as a list on the screen?)
  • Take a look at using CursorLoader , which performs asynchronous tasks such as retrieving a database. This guide introduces the ContentProvider Android approach on interfaces. It’s best to read the official URI and ContentProvider for Android documentation if these terms are not significant.
  • If you work with fragments, look at the FragmentStatePagerAdapter instead of the traditional FragmentPagerAdapter . I did not use this adapter, but I read that it only creates an instance of the currently visible fragment, i.e. Not these fragments to the right or left of the currently selected tab.
  • Look at optimizing the query that you are using against the database.
+3
source share

instantiateItem is called when the ViewPager is about to share and needs to be presented. He does not need to actually create everything. I think that ultimately, lazy loading does not work. As I see it, you need to do two things.

1: Download the data in the background when the user reaches your page. Your example claims to be 364 pages (good lord), so I would say to listen to pages using a listener. When you are on page 363, start downloading data for 364. When you have typed 364, start downloading data to 365 and save the data to 363 if the user wants to change them back. If the data loads relatively quickly or the user takes a long time to swap, this should be unreasonable if you use asyncTask or a stream to load data.

2: specify the default backup, which will not be filled until the data is restored. You will need to do this with option 1, as well as if the user loads the page before you retrieve the data. Basically, just look at what it says “loading ...” or something else until you have data. Either this, or fill in real-time data as you receive it. In this case, the user will see it.

In any case, I think you will need to cache something to make the application look good.

+1
source share

I had a similar problem. Viewpager that loaded heavy data. If you are not going to frequently change the appearance of individual pages, I would suggest that you save the pages in memory. Use the following code to execute this

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

If you want to update data on viewpager slides, use

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

And use the FragmentStatePagerAdapter.

+1
source share

I do not think that any way of lazy loading data is synchronously described. AsyncTask is a way to go, or maybe you can use streams directly. I believe that AsyncTask was designed specifically for this kind of function. It is easier then to use the stream directly. If you need implementation ideas, check out: http://geekjamboree.wordpress.com/2011/11/22/asynctask-call-web-services-in-android/

0
source share

Have you looked into the Android ignition library ? according to Sample-applications there is an Endless List component and an http-cache component.

I have not tried this myself and I do not know if this is a solution for you - I just saw examples .....

0
source share

All Articles