Fragments inside view pager doesn't load the data after the first time

问题: I have view pager contains 3 fragments when I open one of the 3 fragments in the first time it loads the data inside it but when I go to another fragment and reopen the old...

问题:

I have view pager contains 3 fragments when I open one of the 3 fragments in the first time it loads the data inside it but when I go to another fragment and reopen the old fragment the data doesn't load.

this is the code of the viewpagerAdapter :

public class ViewPagerAdapter extends FragmentPagerAdapter {
Context context;

public ViewPagerAdapter(FragmentManager fragmentManager, Context context) {
    super(fragmentManager);
    this.context = context;
}

/**
 * @return the number of pages to display
 */
@Override
public int getCount() {
    return 3;
}

// BEGIN_INCLUDE (pageradapter_getpagetitle)

/**
 * Return the title of the item at {@code position}. This is important as what this method
 * <p>
 * Here we construct one using the position value, but for real application the title should
 * refer to the item's contents.
 */
@Override
public CharSequence getPageTitle(int position) {
    String title = "";
    switch (position) {
        case 0:
            title = "تعلم";
            break;
        case 1:
            title = "الصفحة الرئيسية";
            break;
        case 2:
            title = "التحديات";
            break;
    }
    return title;
}
// END_INCLUDE (pageradapter_getpagetitle)

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new ChallengersFragment();
        case 1:
            return new HomeFragment();
        case 2:
            return new ChallengesFragment();
    }

    return null;
}

}

回答1:

I solved the problem by override setUserVisibleHint method and redownload the data in it .This method will be called every time the visibility of the fragment changes . this is the code :

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        presenter.getUserData();
    }
}
  • 发表于 2019-02-17 16:55
  • 阅读 ( 174 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除