I have Main Activity with View Pager inside that activity. So, I have also FragmentStatePager adapter for handling which fragment is display inside ViewPager.
Anyway, as you know ViewPager loads previous, current and next fragment in memory and that's problem because I want to change content values in next fragment from current fragment based on user interaction from current fragment.
public class StageOneActivity extends FragmentActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_stage_one);
//ViewPager for Introduction
mStageOnePager = (ViewPager) findViewById(R.id.vp_stage_one);
mStageOnePager.setAdapter(new IntroFragmentPagerAdapter(getSupportFragmentManager()));
mStageOnePager.setOffscreenPageLimit(1);
//mStageOnePager.setCurrentItem(8);
}
private class IntroFragmentPagerAdapter extends FragmentStatePagerAdapter {
public IntroFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0:
return StageOneFragment.newInstance(getResources().getColor(R.color.green), R.drawable.stage1_01);
case 1:
return StageOneFragment.newInstance(getResources().getColor(R.color.green), R.drawable.stage1_02);
case 2:
return TitleSpinnerFragment.newInstance(getResources().getColor(R.color.green), R.drawable.stage1_03, R.drawable.bkg_page_1_50);
case 3:
return CurrencySpinnerFragment.newInstance(getResources().getColor(R.color.green), R.drawable.stage1_03, R.drawable.pack_of_20);
case 4:
return SimpleSpinnerFragment.newInstance(getResources().getColor(R.color.green), R.drawable.stage1_05, R.drawable.bkg_page_1_50);
case 5:
return MoneySpentFragment.newInstance(getResources().getColor(R.color.green), R.drawable.money_spent_1, R.drawable.money_spent_2, "0");
case 6:
return StageOneFragment.newInstance(getResources().getColor(R.color.green), R.drawable.stage1_07);
How I can send data to fragment who is already loaded and content is added in ViewPager?
I decided to try to use ViewPager.setOnPageChangeListener and detected when user scrolled to selected screen, but problem is how I can send that data to that Fragment and show it?
You can use eventbus:
https://github.com/greenrobot/EventBus
It's easy to setup and use.
HOWTO with example and code snippets:
You need to define an interface in the fragment and implement it within the Activity.Google documentation:
http://developer.android.com/training/basics/fragments/communicating.html
In your class it'd be something like this:
public Fragment StageOneFragment{
public interface itf{
}
... }
public class StageOneActivity extends FragmentActivity implements StageOneFragment.itf{ ... }
Hope it helps, you should be able to implement the interface that way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.