0

我正在抽屜應用程序與片段。抽屜中的一個選項調用了一個片段,我在其中設置了一個顯示圖形的選項卡式視圖(帶有片段)。Android ViewPager在第一次後不顯示內容

當我第一次運行應用程序時,我的標籤工作得很好,內容,圖形和一切看起來都很棒。

只要我點擊該片段並轉到任何其他片段並返回,標籤就會顯示正確的標題,但不會顯示任何內容,也不會顯示任何圖片。

這裏是樹立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); 
    } 
} 
} 

這裏是我爲它的佈局:

<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> 

這是我的統計(數據)片段中的一個。第二個看起來非常相似:

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); 
} 

我使用appsthatmatter/GraphView進行繪圖。

我試圖調試它,我發現後,我從導航的標籤離開和回來,功能:

@Override 
    public Fragment getItem(int position) { 
     return mFragmentList.get(position); 
    } 

將不會再被調用。它第一次做了兩次,但它永遠不會,直到我正確關閉應用程序並重新啓動。

有人可以幫我弄清楚我做得不對嗎?

謝謝

回答

2

我居然修好了。我不得不改變:

adapter = new ViewPagerAdapter(getFragmentManager()); 

adapter = new ViewPagerAdapter(getChildFragmentManager());