ViewPager with AsyncTaskLoader

与世无争的帅哥 提交于 2019-12-23 05:26:42

问题


Ok, I'm having hard time of understanding what is wrong with my application. So I have ViewPager with Tabs like this:

Adapter.java

public class CustomPagerAdapter extends FragmentPagerAdapter {

public CustomPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new Fragment1();
        case 1:
            return new Fragment2();

        // And so on...

@Override
public int getCount() {
    return 4;
}

Here is the xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.env.test.app.MainActivity" />

MainActivity :

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

static final String[] tabTitles = {"test1","test2","test3","tes3"};
CustomPagerAdapter adapter;
ViewPager viewPager;
ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    adapter = new CustomPagerAdapter(getSupportFragmentManager());
    actionBar = getActionBar();
    viewPager = (ViewPager) findViewById(R.id.pager);

    viewPager.setAdapter(adapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for (String tabTitle : tabTitles) {
        actionBar.addTab(actionBar.newTab().setText(tabTitle).setTabListener(this));

    }

    viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            super.onPageScrollStateChanged(state);
        }
    });
} // And 3 more implemented methods of tabs...

In every one fragment I have called AsyncTaskLoader with Jsoup scrape and CardListView population. Now everything works fine, but lets say I'm on page1. I swipe into the page3. Then I swipe back into page1 and the content (ListView) is gone. Now I'm swiping to page3 or page4 again, there is no content too. So, long story short I'm losing my data ( Tried AsyncTask, but I like loader better and the problem is the same, so I dont think problem is in the Loader ). However here is one of the fragments:

public class Fragment1 extends Fragment implements LoaderManager.LoaderCallbacks<ArrayList<Card>> {

SharedPreferences accountData;
String accountID, accountPassword;

public Fragment1() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment1, container, false);
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    accountData = getActivity().getSharedPreferences(otheractivity.filename, 0);
    accountID = accountData.getString("accountID", null);
    accountPassword = accountData.getString("accountPassword", null);

    getActivity().getSupportLoaderManager().initLoader(0, null, this);
}

@Override
public Loader<ArrayList<Card>> onCreateLoader(int id, Bundle args) {
    return new NetworkTask(getActivity(), accountID, accountPassword, getString(R.string.login_url), getString(R.string.target_url));
}

@Override
public void onLoadFinished(Loader<ArrayList<Card>> loader, ArrayList<Card> result) {
    if (result != null) {
        CardArrayAdapter adapter = new CardArrayAdapter(getActivity(), result);
        CardListView cardListView = (CardListView) getActivity().findViewById(R.id.listView_schedule);
        if (cardListView != null) {
            cardListView.setAdapter(adapter);
        }
    } else {
        TextView warning = (TextView) getActivity().findViewById(R.id.textView_sheduleWarning);
        warning.setText("blah blah blah");
    }
}

@Override
public void onLoaderReset(Loader<ArrayList<Card>> loader) {
}

}

Any thoughts whats wrong ? Maybe it's something with PagerAdapter or CardArrayAdapter ? Thanks ;)


回答1:


There are two ways to solve this problem.

  1. Load all the fragments together. for this use pager.setOffscreenPageLimit(numberOfFragments); immediate after setting the adapter to viepager.

  2. Remove the code from onCreate() to onCreateView() As when you swipe through the viewpager, fragment is reloaded and its OncreateView() is called.



来源:https://stackoverflow.com/questions/23138613/viewpager-with-asynctaskloader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!