Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android load data asynchronously in view pager

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?

like image 367
Hanry Avatar asked May 22 '12 13:05

Hanry


People also ask

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

How do I stop ViewPager from sliding on Android?

-Finally, you can disable it: view_pager. disableScroll(true); or enable it: view_pager. disableScroll(false);

How do I know if my ViewPager is scrolling left or right android?

You save the current page of the ViewPager in OnPageSelected() and compare it to the position parameter of OnPageScrolled() . If the current page is less than or equal to the position , you are scrolling to the right, if not, you are scrolling to the left.

What is the use of view pager in Android?

ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.


2 Answers

I would suggest to work with Fragments (and not directly with views).

You need an Interface on your fragments to tell them when they are shown:

public interface IShowedFragment {

    public void onShowedFragment();
}

Make all your fragments implement that interface, and in that method call your loaders/asyncTasks/background tasks.

Then put an onPageChangeListener on your ViewPager, and when you detect the user changed the page, call the interface method on your fragment. You have some choices with this listener, with one of the methods you can wait for the viewPager to stop to slide to trigger your interface call.

To be able to get the right fragment to make this call, take the fragment from yourFragmentApadter.instantiateItem(ViewGroup, int) which will return the fragment for that 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();
      }
  }
  (...)

Like that you can prepare your fragments with empty views and when you slide on one, you start to load the data.

like image 158
galex Avatar answered Oct 13 '22 00:10

galex


I have just completed a very similar task. To get you started on finding the solution to your problem consider the following points in order;

  1. Look at whether you need to be fetching all of that data in the first instance. Feel free to post back with some detail as to what information you are needing to be loaded and what you are doing with it (displaying it as a list on screen?)
  2. Look at using CursorLoaders which perform heavy-lifting tasks such as database fetches asynchronously. This tutorial on the interwebs introduces the ContentProvider Android approach. Best to familiarise yourself with the official Android URI and ContentProvider documentation if those terms don't mean much.
  3. If you are working with Fragments - Look at using the FragmentStatePagerAdapter instead of the traditional FragmentPagerAdapter. I haven't used this adapter but I have read that it only instantiates the currently visible Fragment, i.e. not those Fragments to the right or left of the currently selected tab.
  4. Look at optimising the query you are running against the DB.
like image 40
BrantApps Avatar answered Oct 13 '22 00:10

BrantApps