如何从 ViewPager 的 onPageSelected AsyncTask 到 Fragments?
Posted
技术标签:
【中文标题】如何从 ViewPager 的 onPageSelected AsyncTask 到 Fragments?【英文标题】:How to AsyncTask to Fragments from onPageSelected of ViewPager? 【发布时间】:2014-01-13 18:10:19 【问题描述】:我是 android 编程新手。 我用谷歌搜索和 ***ed,阅读了 android 文档,但没有任何帮助,或者我可能不太理解。
我正在尝试通过 ViewPager 中的 onPageSelected 将 AsyncTask UI 分段。
像 AndroMoney 应用 ViewPager(在 onPageSelected 之前,它显示“正在加载...”,当 onPageSelected 时,它是 AsyncTasked 并加载。):
AndroMoney ViewPager Fragment Screenshoot
但我的代码是:它在滑动时执行异步任务,有时它会在两个片段之间停止,如下所示:
my code viewpager screenshot
与 AndroMoney 应用程序不同,我的代码不会在 PageSelected 上执行异步任务。
这里是 MyFragment.java:
public class MyFragment extends Fragment
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
final StringBuilder buffer=new StringBuilder();
View v;
public static final MyFragment newInstance(int message)
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putInt(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
v = inflater.inflate(R.layout.myfragment_layout, container, false);
new AsyncDatabase().execute();
return v;
public class AsyncDatabase extends AsyncTask<Void,Void,Void>
@Override
protected Void doInBackground(Void... params)
int message = getArguments().getInt(EXTRA_MESSAGE);
BibleService bService=new BibleServiceImpl(getActivity().getApplication());
List<Bible> bible=bService.getBibleByChapterNumber("Salm", message);
for(Bible b: bible)
for(int i=0;i<10;i++)
buffer.append(b);
return null;
@Override
protected void onPostExecute(Void result)
TextView txt1=(TextView)v.findViewById(R.id.textView1);
TextView txt2=(TextView)v.findViewById(R.id.textView2);
txt1.setText("Done");
txt2.setText(buffer.toString());
@Override
protected void onPreExecute()
TextView txt1=(TextView)v.findViewById(R.id.textView1);
txt1.setText("Loading...");
@Override
protected void onProgressUpdate(Void... values)
这里是 PageViewActivity.java:
public class PageViewActivity extends FragmentActivity
MyPageAdapter pageAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setOffscreenPageLimit(1);
pager.setAdapter(pageAdapter);
pager.setCurrentItem(11);
pager.setOnPageChangeListener(new OnPageChangeListener()
public void onPageScrollStateChanged(int state)
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels)
public void onPageSelected(int position)
);
private List<Fragment> getFragments()
List<Fragment> fList = new ArrayList<Fragment>();
for (int j = 2; j < 150; j++)
fList.add(MyFragment.newInstance(j));
return fList;
private class MyPageAdapter extends FragmentStatePagerAdapter
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments)
super(fm);
this.fragments = fragments;
@Override
public Fragment getItem(int position)
return this.fragments.get(position);
@Override
public int getCount()
return this.fragments.size();
我通过添加以下代码进行了愚蠢的测试:
public void onPageSelected(int position)
new MyFragment().new AsyncDatabase().execute();
没用。:-P
那么,如何像 AndroMoney 应用一样从 PageViewActivity 的 onPageSelected 异步任务到片段。
【问题讨论】:
【参考方案1】:在您的片段中公开一个新方法:
public void executeAsyncTask()
new AsyncDatabase().execute();
在活动中,使您的片段列表最终化,以便您可以在适配器中访问它:
final List<MyFragment> fragments = getFragments();
然后将您的 onPageSelected() 方法更改为:
public void onPageSelected(int position)
fragments.get(position).executeAsyncTask();
您还需要更新 MyPagerAdapter 以使用 MyFragment 而不是 Fragment:
private class MyPageAdapter extends FragmentStatePagerAdapter
private List<MyFragment> fragments;
public MyPageAdapter(FragmentManager fm, List<MyFragment> fragments)
super(fm);
this.fragments = fragments;
@Override
public Fragment getItem(int position)
return this.fragments.get(position);
@Override
public int getCount()
return this.fragments.size();
private List<MyFragment> getFragments()
List<MyFragment> fList = new ArrayList<MyFragment>();
for (int j = 2; j < 150; j++)
fList.add(MyFragment.newInstance(j));
return fList;
【讨论】:
以上是关于如何从 ViewPager 的 onPageSelected AsyncTask 到 Fragments?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 ViewPager 的 onPageSelected AsyncTask 到 Fragments?
如何从(和替换)TabLayout 和 ViewPager 移动到新片段?
如何从 viewpager2 访问 RecyclerViews?