我正在处理带有碎片的抽屉应用程序。抽屉中的一个选项调用一个片段,在其中我设置了一个显示图形的标签视图(带有片段)。
当我第一次运行应用程序时,我的标签工作得很好,内容,图形和一切看起来都很棒。
只要我点击该片段并转到任何其他片段,然后返回,标签会显示正确的标题,但没有内容,没有图表。
这是我设置viewpager的主要片段:
Sub copySheets()
Dim i As integer
Dim n As integer 'the amount of sheets
n = Cells(4, 2).Value 'b4
For i = 2 To n
If ActiveWorkbooks.Worksheets.Count < n Then 'Makes sure the sheets exists
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:= _
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
End If
ws1.Copy ThisWorkbook.Sheets(Sheets.Count) 'copy data
Next i
End Sub
这是我的布局:
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);
}
}
}
这是我的Stats()片段之一。第二个看起来非常相似:
<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>
我使用appsthatmatter / GraphView进行绘图。
我试过调试它,然后我发现在离开标签然后返回后,功能:
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);
}
不再被叫了。它第一次执行两次,但在我正确关闭应用程序并重新启动之前它永远不会。
有人可以帮我弄清楚我做得不对吗?
由于
答案 0 :(得分:3)
我实际修好了它。我不得不改变:
adapter = new ViewPagerAdapter(getFragmentManager());
到
adapter = new ViewPagerAdapter(getChildFragmentManager());