Android Fragment getItem被调用两次导致数据无法显示?

时间:2016-10-19 08:48:53

标签: java android android-fragments

我的Android Fragment存在此问题。发生的事情是我的Fragment中有一些TabLayout。我实例化它们没有问题,但我遇到了一个问题,我第一次实例化Fragment时,创建了Fragment并且数据似乎最初正确加载。但是,在最初加载所有数据之后,第二次调用实例化Fragment,因为出于某种原因再次调用Fragment getItem(int position)方法。但是,因为我只是第一次初始化数据(我在Fragment中创建了一个为我初始化数据的static变量),static变量已经知道数据已经存在了正确加载,不应再次加载数据。结果,我的加载屏幕似乎连续出现,因为第二次无法获取数据。有没有办法解决这个问题?要么只将片段实例化一次,要么仅在创建Fragment的第二次和最后一次加载数据?这是我的代码:

private void setupTabLayout() {
    View layout = findViewById(R.id.activity_main);
    if (layout != null) {
        tabLayout = (TabLayout) layout.findViewById(R.id.tab_layout);
    }
    tabLayout.addTab(tabLayout.newTab().setText("First Fragment"));
    tabLayout.addTab(tabLayout.newTab().setText("Second Fragment"));
    tabLayout.addTab(tabLayout.newTab().setText("Third Fragment"));
    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    if(viewPager != null) {
        viewPager.setOffscreenPageLimit(3);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}

PagerAdapter.java

public class PagerAdapter extends FragmentStatePagerAdapter{
    int numTabs;

    public PagerAdapter(FragmentManager fm, int numTabs) {
        super(fm);
        this.numTabs = numTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                //Here, the PublicTabFragment is instantiated twice,
                //causing the onResume and onCreate methods of my PublicTabFragment() 
                //to be called twice.
                PublicTabFragment tab1 = new PublicTabFragment();
                return tab1;
            case 1:
                FollowersTabFragment tab2 = new FollowersTabFragment();
                return tab2;
            case 2:
                ProfileTabFragment tab3 = new ProfileTabFragment();
                return tab3;
            default:
                return null;
        }
    }

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

PublicTabFragment:

private View fragmentView;
private View progressOverlay;
//Declared static so I don't have to call getPublicPostsFromDatabase twice
private static LocationService locationService;
private static Location currentLocation;

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

@Override
public void onResume() {
    super.onResume();
    setupLocation();
}

private void setupLocation() {
    Context context = getContext();
    //Checked once staticly so I don't have to call getPublicPostsFromDatabase twice
    if(locationService == null) {
        locationService = new LocationService(context, new LocationUpdateListener() {
            @Override
            public void canReceiveLocationUpdates() {

            }

            @Override
            public void cannotReceiveLocationUpdates() {
                //well we know we cant receive updates so we have to create a settings request
            }

            //update location to our servers for tracking purpose
            @Override
            public void updateLocation(Location location) {
                if (location != null) {
                    currentLocation = location;
                    progressOverlay = fragmentView.findViewById(R.id.progress_overlay);
                    progressOverlay.setVisibility(View.VISIBLE);
                    swipeContainer = (SwipeRefreshLayout) fragmentView.findViewById(R.id.swipeContainer);
                    swipeContainer.setColorSchemeResources(R.color.colorPrimary);
                    databaseQuery.getPublicPostsFromDatabase(currentLocation, progressOverlay, fragmentView, getContext(), R.id.public_posts_feed);
                }
            }

            @Override
            public void updateLocationName(String localityName, Location location) {
                locationService.stopLocationUpdates();
            }
        });
        locationService.startUpdates();
    }
}

编辑:我尝试将locationService变量NOT设置为静态,并且第二次实例化片段时getPublicPostsFromDatabase调用正常工作,但问题变为getPublicPostsFromDatabase方法被调用两次。即使我在static boolean variable中设置truegetPublicPostsFromDatabase,而getPublicPostsFromDatabasestatic boolean variable时仅调用false,{{1}只有在Fragment完成后才会再次实例化,然后再次调用getPublicPostsFromDatabase

2 个答案:

答案 0 :(得分:0)

您是否尝试使用布尔值?

private Boolean mLoad = false;

当他们加载时,将他置于真实状态并为负载设置此条件

if (!mLoad) {
    ...
    mLoad = true;
}

答案 1 :(得分:0)

设置setOffscreenPageLimit以便不再次加载片段数据。请尝试以下行:

viewPager.setOffscreenPageLimit(yourNooftab);