Last active
July 1, 2020 16:09
-
-
Save rgdevment/e8de4e375fbf21f1ee3965697e0ac651 to your computer and use it in GitHub Desktop.
[Fragment ViewPager] OnResume, OnPause with ViewPager #Android #Java #ViewPager #Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EnlacesActivity extends FragmentActivity { | |
private FragmentPageAdapter pageAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_enlace); | |
FragmentPageAdapter adapter = new FragmentPageAdapter(getSupportFragmentManager()); | |
pageAdapter = new FragmentPageAdapter(getSupportFragmentManager()); | |
mPager.setPageTransformer(true, new ZoomOutPageTransformer()); | |
mPager.setAdapter(adapter); | |
mPager.setOnPageChangeListener(pageChangeListener); | |
} | |
private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() { | |
int currentPosition = 0; | |
@Override | |
public void onPageSelected(int newPosition) { | |
InterfacesClass.FragmentLifecycle fragmentToHide = (InterfacesClass.FragmentLifecycle)pageAdapter.getItem(currentPosition); | |
fragmentToHide.onPauseFragment(); | |
InterfacesClass.FragmentLifecycle fragmentToShow = (InterfacesClass.FragmentLifecycle)pageAdapter.getItem(newPosition); | |
fragmentToShow.onResumeFragment(); | |
currentPosition = newPosition; | |
} | |
@Override | |
public void onPageScrolled(int arg0, float arg1, int arg2) { } | |
public void onPageScrollStateChanged(int arg0) { } | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FragmentPageAdapter extends FragmentPagerAdapter { | |
private List<Fragment> fragments; | |
public FragmentPageAdapter(FragmentManager fm) { | |
super(fm); | |
this.fragments = new ArrayList<>(); | |
fragments.add(new PageZero()); | |
fragments.add(new PageZero()); | |
fragments.add(new PageZero()); | |
//... Otros Fragments | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return fragments.get(position); | |
} | |
@Override | |
public int getCount() { | |
return fragments.size(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InterfacesClass { | |
public interface FragmentLifecycle { | |
public void onPauseFragment(); | |
public void onResumeFragment(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PageOne extends Fragment implements InterfacesClass.FragmentLifecycle { | |
private static final String TAG = PageOne.class.getSimpleName(); | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_page_one, container, false); | |
return view; | |
} | |
@Override | |
public void onPauseFragment() { | |
Log.i(TAG, "onPauseFragment()"); | |
//Save to DataBase | |
} | |
@Override | |
public void onResumeFragment() { | |
Log.i(TAG, "onResumeFragment()"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment