English 中文(简体)
和机器人载荷数据在查看器PLCr 中不同步
原标题: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.

我的代码样本如下:

 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;
}

我是在用创造方法来称呼这个的。

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

有什么解决办法吗?

问题回答

我建议与不成体系问题(而不是直接与意见问题)合作。

您需要您碎片上的接口,以便在显示时告诉他们:

public interface IShowedFragment {

    public void onShowedFragment();
}

使所有碎片都执行该接口, 使用该方法将您的装载器/ asyncTassks/ 后台任务调用 。

然后在您的 ViewPager 上安装一个 Page- Change 听力器, 当你检测到用户更改了页面时, 请调用您碎片上的界面方法。 您可以使用这个倾听器选择一些方法, 其中一种方法是您可以等待查看Pager 停止滑动以触发您的界面呼叫 。

要获得正确的碎片来进行此调用, 请从您的分散式自动调用器中取出该碎片。 InstantantiateTroup( ViewGroup, int) 。 如果已经装入, 它将返回该位置的碎片 。

mPager.setOnPageChangeListener(new OnPageChangeListener() {

  @Override
  public void onPageSelected(int position) {

      Fragment fragment = (Fragment) mAdapter.instantiateItem(mPager, position);
      if(fragment instanceof IShowedFragment){
           ((IShowedFragment) fragment).onShowedFragment();
      }
  }
  (...)

比如,您可以用空视图来准备碎片, 当您滑行在一边时, 您开始装入数据 。

我刚刚完成了一项非常相似的任务。

  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.

interantantantantitlem 被调用时, ViewPager 即将互换, 需要查看。 它没有 /em 来实际创造一切。 我认为, 最终, 懒惰的加载已经退出了。 在我看来, 您需要在这里做两件事 。

1: Cache the data in the background when the user is about to reach your page. Your example claims that 364 pages (good Lord), so I d say use a listener to handle page changes. When you re at page 363, start loading the data for 364. When you re at 364, start loading the data at 365 and keep the data at 363 in case the user wants to swap back. If the data loads relatively quickly or the user takes a long time to swap, it should be seemless assuming you re using asyncTask or thread to load the data.

2: 拥有一个在数据检索之前不会被覆盖的备份默认视图。 您需要使用选项1 进行此操作, 以防用户在检索数据之前加载页面。 基本上, 只要有一个“ 装入... ” 或某种东西的视图, 直至您掌握数据 。 要么是这个视图, 要么是当您获得数据时实时填充数据 。 如果用户看到它建立的话 。

无论哪种方式, 我认为你需要隐藏的东西 使应用程序看起来不错。

我也有类似的问题。一个装有重数据的查看页面。如果你不经常更改单个页面的视图,我建议你保留这些页面的记忆。使用以下代码来进行此操作

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

如果您想要更新查看页面幻灯片上的数据,请使用

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

并使用零碎的状态PagerAdapter。

I do not think there s any way to lazy load data in the synchronous fashion that you describe. AsyncTask is the way to go or perhaps you could use threads directly. I believe AsyncTask was designed specifically for this kind of functionality. It s easier to use then the thread directly. If you need ideas on implementation, have a look at: http://geekjamboree.wordpress.com/2011/11/22/asynctask-call-web-services-in-android/

您是否查看了 < a href=> "https://github.com/kaeppler/ignimation" rel="nofollow" >and mid点火图书馆 ? 根据Sample-applications , 有一个“无尽列表”和http-cache 组件。

我还没自己尝试过 不知道这是否是解决你的办法... 刚刚看到了这些例子...





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...