I am working on a drawer app with fragments. One of the options in the drawers calls a fragment in which I set up a tabbed view (with fragments) that show graphs.
When I run the app for the first time, my tabs work perfectly fine, contents, graph and everything looks great.
As soon as I click away from that fragment and go to any other fragment, and come back, the tabs show up with correct titles, but no contents, no graph nothing.
Here is my main fragment that sets up the viewpager:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class StatsMain extends Fragment {
private ViewPagerAdapter adapter = null;
private TabLayout tabLayout = null;
private ViewPager viewPager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.stats_main, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new Stats1(), "Stats 1");
adapter.addFragment(new Stats2(), "Stats 2");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
And here is my layout for it:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
This is one of my Stats() fragments. The second one looks very similar:
public class Stats1 extends Fragment {
GraphView graph;
public Stats1 () {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.stats1, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
Calendar calendar = Calendar.getInstance();
Date d1 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d2 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d3 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d4 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d5 = calendar.getTime();
graph = (GraphView) view.findViewById(R.id.graph);
StaticLabelsFormatter staticLabelsFormatter = new
StaticLabelsFormatter(graph);
staticLabelsFormatter.setHorizontalLabels(new String[] {"old", "middle", "new"});
graph.setTitle("Data History");
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(d1, 145.4),
new DataPoint(d2, 145.9),
new DataPoint(d3, 147.2),
new DataPoint(d4, 146.5),
new DataPoint(d5, 147.1)
});
graph.getViewport().setScalable(true);
graph.addSeries(series);
// set date label formatter
//graph.getGridLabelRenderer().setLabelFormatter(new
DateAsXAxisLabelFormatter(this));
graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
//graph.getGridLabelRenderer().setNumHorizontalLabels(6); // only 4 because of the space
series.setDrawDataPoints(true);
// set manual x bounds to have nice steps
graph.getViewport().setMinX(d1.getTime());
graph.getViewport().setMaxX(d5.getTime());
graph.getViewport().setXAxisBoundsManual(true);
// as we use dates as labels, the human rounding to nice readable numbers
// is not necessary
graph.getGridLabelRenderer().setHumanRounding(false);
}
I use appsthatmatter/GraphView for graphing purposes.
I tried debugging it and I found out that after I navigate away from the tabs and come back, the function:
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
won't get called anymore. It does twice the first time around but it never will until i properly close the app and restart.
Can anybody help me figure out what I'm not doing right, please?
Thanks
I actually fixed it. I had to change:
adapter = new ViewPagerAdapter(getFragmentManager());
to
adapter = new ViewPagerAdapter(getChildFragmentManager());
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