鉴于寻呼机的Andr​​oid负荷数据不同步 [英] android load data asynchronously in view pager

查看:167
本文介绍了鉴于寻呼机的Andr​​oid负荷数据不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的视图寻呼机实施的解决方案。 首先我从数据​​库中的单页刷卡过程中加载大量数据,所以有时它会减慢刷卡频率(你需要多时间刷页)在后台是做取任务。 我没有使用异步任务返回的视图。 有没有什么办法可以延迟加载页面只允许用户进入其它页面上刷卡,但数据延迟加载。

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.

我的code样品如下;

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?

推荐答案

我会建议用碎片的工作(而不是直接与意见)。

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

请您所有的片段实现该接口,并在该方法中调用您的装载机/ asyncTasks /后台任务。

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

然后把你的ViewPager的onPageChangeListener,当你发现用户更改页面,请您片段的接口方式。你有一些选择,这个监听器,具有可等待viewPager的方法之一停止滑动来触发你的接口调用。

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.

要能够得到正确的片段,使这个电话,把片段yourFragmentApadter.instantiateItem(ViewGroup中,INT),如果它已经加载,这将返回片段的位置。

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();
 }
 (...)

}

就像你可以prepare您片段空的观点,当你在一个幻灯片,你开始加载数据。

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

这篇关于鉴于寻呼机的Andr​​oid负荷数据不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆