仅按时加载片段数据

时间:2012-02-09 03:07:25

标签: android android-viewpager

class GoogleMusicAdapter extends TestFragmentAdapter implements TitleProvider {
    public GoogleMusicAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        if(position == 0)
            return MyClass.newInstance();
        else if(position == 1)
            return MyClass.newInstance();
        else
            return MyClass.newInstance();

    }

    @Override
    public int getCount() {
        return SampleTabsStyled.CONTENT.length;
    }

    @Override
    public String getTitle(int position) {
        return SampleTabsStyled.CONTENT[position % SampleTabsStyled.CONTENT.length];
    }

}

我的MyClass类中有一个AsyncTask。我在我的viewpager中使用了这个GoogleMusicAdapter类。每当我将页面更改为另一个时,它再次运行asynctask但我不想要它。它必须在start和mustn上运行我再叫一次。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

让它成为单身人士吗?

private static class MyAsyncTask extends AsyncTask<Void,Void,Void> {
    private MyAsyncTask mInstance;

    public static boolean executeIfNotCreated() {
        if(mInstance == null) {
            mInstance = new MyAsyncTask();
            mInstance.execute();
            return true;
        } 
        return false;
    }
    ...
}