I'm building an App that use Fragment and a single MainActivity
that showing different fragments in the same ViewPager to replicate the behaviour of a typical MVC application.
The problem is that haven't understood how could I update the fragment programmatically to update the ViewPager
with the new Fragment changes
For example I have this Fragment
that show a chart, (in my intention) when I invoke setFragment(int interval, Point[] snodePoint)
from the main, the chart should be updated in ViewPager
public class LineFragment extends Fragment { static int interval; static Point snodePoints[]; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View v = inflater.inflate(R.layout.fragment_linegraph, container, false); Bundle bundle = this.getArguments(); ChartConfig cg = bundle.getParcelable("FragmentData"); snodePoints = cg.getPoints(); interval= cg.getInterval(); Line l = new Line(); for(int i=0; i<snodePoints.length; i++){ LinePoint p = new LinePoint(); Point sp=snodePoints[i]; p.setX(sp.getX()); p.setY(sp.getY()); l.addPoint(p) } l.setColor(getResources().getColor(R.color.green)); LineGraph li = (LineGraph) v.findViewById(R.id.linegraph); li.addLine(l); li.setRangeY(0, interval); li.setLineToFill(0); li.setOnPointClickedListener(new OnPointClickedListener() { @Override public void onClick(int lineIndex, int pointIndex) { // TODO Auto-generated method stub } }); return v; } }
In few words I need to
How could I do this?
The Main Activity with the FragmentAdapter
public class MainActivity extends FragmentActivity { ViewPager mViewPager; MyFragmentAdapter mMyFragmentAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewPager = (ViewPager) findViewById(R.id.view_pager); final ActionBar bar = this.getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); LineFragment lineFrag = new LineFragment(); BarFragment barFrag = new BarFragment(); PieFragment pieFrag = new PieFragment(); mMyFragmentAdapter = new MyFragmentAdapter(this, mViewPager); mMyFragmentAdapter.addTab(bar.newTab().setText("Line"), LineFragment.class, null, lineFrag); mMyFragmentAdapter.addTab(bar.newTab().setText("Bar"), BarFragment.class, null, barFrag); mMyFragmentAdapter.addTab(bar.newTab().setText("Pie"), PieFragment.class, null, pieFrag); mViewPager.setOffscreenPageLimit(mMyFragmentAdapter.getCount() - 1); } public static class MyFragmentAdapter extends FragmentStatePagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener { private final MainActivity mContext; private final ActionBar mActionBar; private final ViewPager mViewPager; private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); private final ArrayList<Fragment> mFrag = new ArrayList<Fragment>(); static final class TabInfo { private final Class<?> clss; private final Bundle args; TabInfo(Class<?> _class, Bundle _args) { clss = _class; args = _args; } } public MyFragmentAdapter(MainActivity activity, ViewPager pager) { super(activity.getSupportFragmentManager()); mContext = activity; mActionBar = activity.getActionBar(); mViewPager = pager; mViewPager.setAdapter(this); mViewPager.setOnPageChangeListener(this); } public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args, Fragment frag) { TabInfo info = new TabInfo(clss, args); tab.setTag(info); tab.setTabListener(this); mTabs.add(info); mFrag.add(frag); mActionBar.addTab(tab); notifyDataSetChanged(); } @Override public int getCount() { return mTabs.size(); } @Override public Fragment getItem(int position) { TabInfo info = mTabs.get(position); Bundle args = new Bundle(); ChartConfig cg = new ChartConfig(interval, snodePoint); args.putParcelable("FragmentData", cg); Fragment f = mFrag.get(position); f.setArguments(args); return f; } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { mActionBar.setSelectedNavigationItem(position); } @Override public void onPageScrollStateChanged(int state) { } public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) { Object tag = tab.getTag(); for (int i = 0; i < mTabs.size(); i++) { if (mTabs.get(i) == tag) { mViewPager.setCurrentItem(i); } } } public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) { } @Override public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) { } }
}
The first time that I create the fragment, the main activity pass successful the parameters to the Fragment through the overrided getItem(int position)
, but if I try to change the data, also if I try to invoke notifyDataSetChanged()
on the adapter mMyFragmentAdapter
, nothing happens and the fragments in ViewPager
continue to use the same data passed in creation.
EDIT
Following some partial code seen in other question about fragment update I have also tried this
public void update() { try{ LineFragment fragment = (LineFragment) getSupportFragmentManager().findFragmentByTag( "android:switcher:"+R.id.view_pager+":0"); if(fragment != null) { if(fragment.getView() != null) { fragment.updateDisplay(); } } } catch(RuntimeException e){ e.printStackTrace(); } }
Assuming that 0 is the id of the first Fragment-Tab fragment(I'm not sure about this kind of operation), but fragment.updateDisplay();
is not invoked.
Using the ViewPager. OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager.
Firstly set viewpager adapter to null then recreate the adapter and set i to it to viewpager. Show activity on this post. Define which message which is needed to update.
In your Adapter override getItemPosition
@Override public int getItemPosition(Object object) { // POSITION_NONE makes it possible to reload the PagerAdapter return POSITION_NONE; }
After that
viewPager.getAdapter().notifyDataSetChanged()
should work.
I need to understand how the Fragment can get the data passed by main, maybe I'm missing something but really I haven't understood how could I do this.
From this it seems that you want to pass the data at the moment of the creation of the Fragment
. If this is what you want you shouldn't make a custom constructor(and generally for a Fragment
you should have only the default no-args constructor(because the platform needs it to recreate the Fragment
at certain times)).
Instead you should either pass the data in a Bundle
:
@Override public Fragment getItem(int position) { switch (position) { //... Bundle args = new Bundle(); args.put("a_string_representing_theKey", data) LineFragment f = new LineFragment(); f.setArguments(args); return f; //... } }
This will only work if the data can be put in a Bundle
(see the put...
method of the Bundle
class, all primitives + objects that are Parcelable
). In the fragment you could then retrieve the arguments using getArguments()
.
You can also make the Fragment
retrieve the data directly from the Activity
:
// in the lifecycle method where you want the data ((MainActivity)getActivity()).getDataForFragment(); // use the data
For example I have this Fragment that show a chart, (in my intention) when I invoke setFragment(int interval, Point[] snodePoint) from the main, the chart should be updated in ViewPager
You seem to contradict yourself. For this use the code from the question I linked to, of course just calling your current setFragment()
method will not do anything as you just update the value of two variables, you'll need to let the UI know about the change(like calling another method to recreate the view hierarchy, setting the new values on the chart view and calling invalidate()
etc).
Edit:
The code used in the question I linked to should work, but you aren't using the proper code. You have a FragmentStatePagerAdapter
but you're using the code for a FragmentPagerAdapter
which will not work:
FragmentStatePagerAdapter a = (FragmentStatePagerAdapter) mViewPager.getAdapter(); LineFragment f = (LineFragment) a.instantiateItem(pager, thePositionYouWant);
Keep in mind that the code above will work only for the visible fragment plus 1 fragment on each side, the other fragments will not be available(and as the user doesn't see them or can't move to them right away they shouldn't be updated).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With