How to handle network calls inside a Fragment

前端 未结 7 1043
攒了一身酷
攒了一身酷 2021-01-31 23:21

I have the following situation:

I have an Activity that hosts a ViewPager, and I have 4 Fragments;

the ViewPager

7条回答
  •  独厮守ぢ
    2021-02-01 00:01

    Create a page into view method for FragmentStatePagerAdapter which calls a method on the fragment when the fragment comes into view.

    Implement the OnPageIntoView interface in your fragment.

    public class SomethingDifferent extends Fragment implements OnPageIntoView {
    ...
    /*
     * Called when this page comes into view
     * 
     * @see com.gosylvester.bestrides.SettingFragmentPagerSupport.MyAdapter.
     * OnPageIntoView#onPageIntoView()
     */
    @Override
    public void onPageIntoView() {
        // this is just some random example code
        // that does some heavy lifting it only runs when the fragment
        // frist comes into view
        if (fragmentActivity != null) {
            if (lrc == null) {
                lrc = new ClientServiceLocationRecorder(
                        new WeakReference(
                                fragmentActivity.getApplicationContext()),
                        lrcCallback);
            }
    
            // get a status message from the location recorder
            lrc.sndMessageToLocationRecorder(ServiceLocationRecorder.MSG_RECORD_STATUS);
        }
    }
    

    Create a custom FragmentStatePagerAdapter Override the setPrimaryItem method and if the object can be cast to the interface then call through the interface one time only.

    public static class MyAdapter extends FragmentStatePagerAdapter {
    
        public interface OnPageIntoView {
            public void onPageIntoView();
        }
    
        private Fragment mCurrentFragment;
    
        //bonus method to get the current fragment
        public Fragment getCurrentFragment() {
            return mCurrentFragment;
        }
    
        static int lastPosition = -1;
    
        @Override
        public void setPrimaryItem(ViewGroup container, int position,
                Object object) {
            //quickly determine if the primary item has changed
            //and one time only call through interface
            if (position != lastPosition) {
                lastPosition = position;
                //determine if this is fragment it should be but lets avoid 
                //class cast exceptions
                if (Fragment.class.isAssignableFrom(object.getClass())) {
                    mCurrentFragment = ((Fragment) object);
                    //determine if the onPageIntoView interface has
                    //been implemented in the fragment
                    //if so call the onPageIntoView
                    if (OnPageIntoView.class.isAssignableFrom(mCurrentFragment
                            .getClass())) {
                        ((OnPageIntoView) mCurrentFragment).onPageIntoView();
                    }
                }
            }
            super.setPrimaryItem(container, position, object);
        }
    }
    

提交回复
热议问题