Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update-refresh Fragment in ViewPager by Main Activity programmatically
    primarykey
    data
    text
    <p>I'm building an App that use Fragment and a single <code>MainActivity</code> that showing different fragments in the same ViewPager to replicate the behaviour of a typical MVC application.</p> <p>The problem is that haven't understood how could I update the fragment programmatically to update the <code>ViewPager</code> with the new Fragment changes</p> <p>For example I have this <code>Fragment</code> that show a chart, (in my intention) when I invoke <code>setFragment(int interval, Point[] snodePoint)</code> from the main, the chart should be updated in <code>ViewPager</code></p> <pre><code>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&lt;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; } } </code></pre> <p>In few words I need to </p> <ul> <li>1-create a fragment based on the data received by the main activity. </li> <li>2-change the data of the fragment everytime in the main activity happens something like setFragment(...) with new data (generated always by the main).</li> </ul> <p>How could I do this?</p> <p>The Main Activity with the FragmentAdapter</p> <pre><code>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&lt;TabInfo&gt; mTabs = new ArrayList&lt;TabInfo&gt;(); private final ArrayList&lt;Fragment&gt; mFrag = new ArrayList&lt;Fragment&gt;(); static final class TabInfo { private final Class&lt;?&gt; clss; private final Bundle args; TabInfo(Class&lt;?&gt; _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&lt;?&gt; 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 &lt; 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) { } } </code></pre> <p>}</p> <p>The first time that I create the fragment, the main activity pass successful the parameters to the Fragment through the overrided <code>getItem(int position)</code>, but if I try to change the data, also if I try to invoke <code>notifyDataSetChanged()</code> on the adapter <code>mMyFragmentAdapter</code>, nothing happens and the fragments in <code>ViewPager</code> continue to use the same data passed in creation.</p> <p><strong>EDIT</strong></p> <p>Following some partial code seen in other question about fragment update I have also tried this</p> <pre><code>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(); } } </code></pre> <p>Assuming that 0 is the id of the first Fragment-Tab fragment(I'm not sure about this kind of operation), but <code>fragment.updateDisplay();</code> is not invoked.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload